length of a string

조회 수: 21 (최근 30일)
William
William 2011년 9월 29일
댓글: Vijay Kamble 2019년 7월 4일
Is there a function that can return the length of a string? I have tried using size. but for some reason it is not going so well.
  댓글 수: 1
Jan
Jan 2011년 9월 29일
Please post an exact error description than "is not going well" in the future. Posting the error message is more helpful.

댓글을 달려면 로그인하십시오.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 29일
str='abcdefe'
size(str)
length(str)
numel(str)
  댓글 수: 3
Jan
Jan 2011년 9월 29일
Especially: size(str, 2)
Vijay Kamble
Vijay Kamble 2019년 7월 4일
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Johannes Kalliauer
Johannes Kalliauer 2018년 1월 11일
If you have string not a char then you have to use a different command to get the number of chars: strlength
str=string('abcdefe')
strlength(str)
or you can convert the string to a char and then deterimise the length with the code posted by @Fangjun Jiang
str=string('abcdefe')
chr=char(str)
size(chr,2)
length(chr)
numel(chr)
but if you are interested in the number of lines use:
strSplited = splitlines(stringWithLinebreaks);
size(strSplited,1)
length(strSplited)
numel(strSplited)
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 1월 11일
Note that in 2011 when the question was originally asked, the string object did not exist and "string" often referred to a character vector.
I think it would have been better if they had used a different datatype name to avoid confusion.

댓글을 달려면 로그인하십시오.


Daniel Shub
Daniel Shub 2011년 9월 29일
Have you created a variable called size? what do you get with
which size
It should be something like ../matlab/r2011a/toolbox/matlab/elmat/size

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by