String Length
조회 수: 9 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!