String Length

9 visualizaciones (últimos 30 días)
Chris
Chris el 27 de En. de 2011
I am trying to find the length of a string. I know should be easy. The string is created in a function call command line, eg: function_name({'string1', 'string21'});
and then the strings are used inside the function. however the strings can be of different lengths. When I do length on the above string I get 2 as the answer. When I do length or size on the element, eg length(string(1)) I get the value 1, no matter how long the string element is. I can print them, compare them but I can't seem to find out how long they are. I have tried doing string(1).length() but that returns an error.
Can anyone help?

Respuesta aceptada

Sebastian
Sebastian el 27 de En. de 2011
Your input variable is a cell array. Each string is an element in that cell array and so length reports 2, because you have 2 elements. If you do length(string(1)) then you still try to access the cell element itself rather than its content. However, if you do length(string{1}) then this should actually report the length of the first string, because using curly bracket you can access the content of a cell. If you search the documentation for "cell array" you will find more details on this.
  1 comentario
Chris
Chris el 27 de En. de 2011
that is brilliant thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by