Manipulating strings defined with the new double quote option

145 次查看(过去 30 天)
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 个评论
Jan
Jan 2017-8-2
Do not get frustrated too soon. Matlab's documentation is marvelous and you can find the required details e.g. here.

请先登录,再进行评论。

回答(5 个)

Jiro Doke
Jiro Doke 2017-8-2
编辑:Jiro Doke 2017-8-2
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 =
"00:06:57"
>> ch = str{1}
ch =
'00:06:57'
>> ch_snippet = str{1}(2:4)
ch_snippet =
'0:0'
  2 个评论
Bart McCoy
Bart McCoy 2022-5-31
编辑:Bart McCoy 2022-5-31
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.

请先登录,再进行评论。


James Tursa
James Tursa 2017-8-2
extractBetween(s1,2,4)

John D'Errico
John D'Errico 2017-8-2
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

MATLAB_User
MATLAB_User 2017-8-2
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 2017-8-8
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 个评论
Jan
Jan 2017-8-8
编辑:Jan 2017-8-9
I think I have written these comments before already. Did you delete your answer and posted it again?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by