Effacer les filtres
Effacer les filtres

String Length

6 vues (au cours des 30 derniers jours)
Chris
Chris le 27 Jan 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?

Réponse acceptée

Sebastian
Sebastian le 27 Jan 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 commentaire
Chris
Chris le 27 Jan 2011
that is brilliant thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by