Manipulating strings defined with the new double quote option

194 visualizaciones (últimos 30 días)
MATLAB_User
MATLAB_User el 2 de Ag. de 2017
Editada: Bart McCoy el 31 de Mayo de 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'
while
s1="00:06:57"; s1(2:4) => produces 'index exceeds matrix dimension' error
  1 comentario
Jan
Jan el 2 de Ag. de 2017
Do not get frustrated too soon. Matlab's documentation is marvelous and you can find the required details e.g. here.

Iniciar sesión para comentar.

Respuestas (5)

Jiro Doke
Jiro Doke el 2 de Ag. de 2017
Editada: Jiro Doke el 2 de Ag. de 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 =
"00:06:57"
>> ch = str{1}
ch =
'00:06:57'
>> ch_snippet = str{1}(2:4)
ch_snippet =
'0:0'
  2 comentarios
Eomzi Yang
Eomzi Yang el 14 de Sept. de 2019
this also works when inserting apostrophe into a string
Bart McCoy
Bart McCoy el 31 de Mayo de 2022
Editada: Bart McCoy el 31 de Mayo de 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.

Iniciar sesión para comentar.


James Tursa
James Tursa el 2 de Ag. de 2017
extractBetween(s1,2,4)

John D'Errico
John D'Errico el 2 de Ag. de 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

MATLAB_User
MATLAB_User el 2 de Ag. de 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 el 8 de Ag. de 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 comentario
Jan
Jan el 8 de Ag. de 2017
Editada: Jan el 9 de Ag. de 2017
I think I have written these comments before already. Did you delete your answer and posted it again?

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by