Is there a function that can return the length of a string? I have tried using size. but for some reason it is not going so well.

1 comentario

Jan
Jan el 29 de Sept. de 2011
Please post an exact error description than "is not going well" in the future. Posting the error message is more helpful.

Iniciar sesión para comentar.

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 29 de Sept. de 2011

1 voto

str='abcdefe'
size(str)
length(str)
numel(str)

3 comentarios

Andrei Bobrov
Andrei Bobrov el 29 de Sept. de 2011
numel
Jan
Jan el 29 de Sept. de 2011
Especially: size(str, 2)
Vijay Kamble
Vijay Kamble el 4 de Jul. de 2019
Thanks

Iniciar sesión para comentar.

Más respuestas (2)

Johannes Kalliauer
Johannes Kalliauer el 11 de En. de 2018

4 votos

If you have string not a char then you have to use a different command to get the number of chars: strlength
str=string('abcdefe')
strlength(str)
or you can convert the string to a char and then deterimise the length with the code posted by @Fangjun Jiang
str=string('abcdefe')
chr=char(str)
size(chr,2)
length(chr)
numel(chr)
but if you are interested in the number of lines use:
strSplited = splitlines(stringWithLinebreaks);
size(strSplited,1)
length(strSplited)
numel(strSplited)

1 comentario

Walter Roberson
Walter Roberson el 11 de En. de 2018
Note that in 2011 when the question was originally asked, the string object did not exist and "string" often referred to a character vector.
I think it would have been better if they had used a different datatype name to avoid confusion.

Iniciar sesión para comentar.

Daniel Shub
Daniel Shub el 29 de Sept. de 2011

0 votos

Have you created a variable called size? what do you get with
which size
It should be something like ../matlab/r2011a/toolbox/matlab/elmat/size

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Sept. de 2011

Comentada:

el 4 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by