Manipulating strings defined with the new double quote option
150 views (last 30 days)
Show older comments
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'
while
s1="00:06:57"; s1(2:4) => produces 'index exceeds matrix dimension' error
1 Comment
Jan
on 2 Aug 2017
Do not get frustrated too soon. Matlab's documentation is marvelous and you can find the required details e.g. here.
Answers (5)
Jiro Doke
on 2 Aug 2017
Edited: Jiro Doke
on 2 Aug 2017
Read up on the documentation on "strings".
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 =
"00:06:57"
>> ch = str{1}
ch =
'00:06:57'
>> ch_snippet = str{1}(2:4)
ch_snippet =
'0:0'
2 Comments
Bart McCoy
on 31 May 2022
Edited: Bart McCoy
on 31 May 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.
John D'Errico
on 2 Aug 2017
Too late, but so what? An alternative is:
S = "234"
S =
"234"
whos S
Name Size Bytes Class Attributes
S 1x1 132 string
See that we cannot index using (), because S is just one object.
S(1)
ans =
"234"
But try this:
S{1}(1)
ans =
'2'
So we can index into a string.
When all else fails...
methods(S)
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
0 Comments
John BG
on 8 Aug 2017
Hi MATLAB_User
you can use a range, don't miss '[' ']'
s1([2:4])
=
'0:0'
or indices
s1([3 6])
=
'::'
To get the figures only
s1(find(s1~=':'))
=
'000657'
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 Comment
Jan
on 8 Aug 2017
Edited: Jan
on 9 Aug 2017
I think I have written these comments before already. Did you delete your answer and posted it again?
- In the first case omitting the square brackets is more efficient, see https://www.mathworks.com/matlabcentral/answers/35676-why-not-use-square-brackets
- In s1(find(s1~=':')) the find can be omitted for using the faster logical indexing.
- The problem of the OP concerned the new string class, not char vectors, such that the indexing methods do not apply here.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!