Filter löschen
Filter löschen

Manipulating strings defined with the new double quote option

202 Ansichten (letzte 30 Tage)
MATLAB_User am 2 Aug. 2017
Bearbeitet: Bart McCoy am 31 Mai 2022
I imported data into MATLAB and the data I want to get is has somehow been converted in a double quote string: "00:06:57" (the type is listed as 'string').
How do I address individual characters defined in such a string? MATLAB only seems to be able to look at the inside of strings with single quote (i.e. '00:06:57') where I could specify a range of characters (this doesn't work with the double quote string function.
I am frustrated! It seems like this should be so easy...but it is not.
s1='00:06:57'; s1(2:4) = '0:0'
s1="00:06:57"; s1(2:4) => produces 'index exceeds matrix dimension' error
  1 Kommentar
Jan am 2 Aug. 2017
Do not get frustrated too soon. Matlab's documentation is marvelous and you can find the required details e.g. here.

Melden Sie sich an, um zu kommentieren.

Antworten (5)

Jiro Doke
Jiro Doke am 2 Aug. 2017
Bearbeitet: Jiro Doke am 2 Aug. 2017
Read up on the documentation on "strings".
It's different from character arrays: Represent Text with Character and String Arrays
What James suggested is correct. If you want to manipulate the "string" as a "character array", you can extract it using {}. Then combine that with regular indexing () to extract individual characters.
>> str = "00:06:57"
str =
>> ch = str{1}
ch =
>> ch_snippet = str{1}(2:4)
ch_snippet =
  2 Kommentare
Eomzi Yang
Eomzi Yang am 14 Sep. 2019
this also works when inserting apostrophe into a string
Bart McCoy
Bart McCoy am 31 Mai 2022
Bearbeitet: Bart McCoy am 31 Mai 2022
If new to strings and you want to see it in steps, I've broken this down a little more.
% If working with a single string object (array with only 1 string obj)
oneStr = "00:06:57"; % One string object (array of 1 string object)
ch = oneStr.char(); % Convert string obj to char array
ch = oneStr{1}; % Convert string obj #1 to char array
% If working with an array of string objects, { } notation makes more sense
strArray = [ "00:06:57", "00:07:23", "00:08:19" ]; % Array of string objects
st = strArray(2) % returns "00:07:23", the 2nd string obj
ch = strArray(2).char() % returns '00:07:23', a char array from the 2nd string obj
ch = strArray{2} % returns '00:07:23', a char array from the 2nd string obj
% much nicer!
ch(4:5) % Char array manipulation- returns '07'
strArray{2}(4:5) % Combining the last 2 steps without the intermediate 'ch'
% Also returns '07'
A key point is that a "string" is an object and you manipulate it (extract substrings, etc) using its methods. An array of string objects is an array of completely different strings, not an array of chars.
Characters are single char objects. It takes a char array to make something that looks like a string.

Melden Sie sich an, um zu kommentieren.

James Tursa
James Tursa am 2 Aug. 2017

John D'Errico
John D'Errico am 2 Aug. 2017
Too late, but so what? An alternative is:
S = "234"
S =
whos S
Name Size Bytes Class Attributes
S 1x1 132 string
See that we cannot index using (), because S is just one object.
ans =
But try this:
ans =
So we can index into a string.
When all else fails...
Methods for class string:
cellstr count erase extractBetween insertBefore le pad reverse startsWith
char double eraseBetween ge ismissing lower plus sort strip
compose endsWith extractAfter gt issorted lt replace split strlength
contains eq extractBefore insertAfter join ne replaceBetween splitlines upper

MATLAB_User am 2 Aug. 2017
Thanks to everyone who responded so quickly. That was amazingly fast!
It's hard to pick the best answer but I think that Steven Lord's answer helped me the most.

John BG
John BG am 8 Aug. 2017
you can use a range, don't miss '[' ']'
or indices
s1([3 6])
To get the figures only
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  1 Kommentar
Jan am 8 Aug. 2017
Bearbeitet: Jan am 9 Aug. 2017
I think I have written these comments before already. Did you delete your answer and posted it again?

Melden Sie sich an, um zu kommentieren.


Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by