String Length

12 views (last 30 days)
Chris
Chris on 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?

Accepted Answer

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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!