Manipulating strings defined with the new double quote option

316 views (last 30 days)
MATLAB_User on 2 Aug 2017
Commented: Eomzi Yang on 14 Sep 2019
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 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.

Sign in to comment.

Answers (5)

jiro on 2 Aug 2017
Edited: jiro on 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 =

John D'Errico
John D'Errico on 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 on 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 on 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 Comment
Jan on 8 Aug 2017
I think I have written these comments before already. Did you delete your answer and posted it again?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!