String Length

9 ビュー (過去 30 日間)
Chris
Chris 2011 年 1 月 27 日
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?

採用された回答

Sebastian
Sebastian 2011 年 1 月 27 日
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 件のコメント
Chris
Chris 2011 年 1 月 27 日
that is brilliant thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by