length of a string

155 views (last 30 days)
William
William on 29 Sep 2011
Commented: Vijay Kamble on 4 Jul 2019
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 Comment
Jan
Jan on 29 Sep 2011
Please post an exact error description than "is not going well" in the future. Posting the error message is more helpful.

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 29 Sep 2011
str='abcdefe'
size(str)
length(str)
numel(str)
  3 Comments
Vijay Kamble
Vijay Kamble on 4 Jul 2019
Thanks

Sign in to comment.

More Answers (2)

Johannes Kalliauer
Johannes Kalliauer on 11 Jan 2018
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 Comment
Walter Roberson
Walter Roberson on 11 Jan 2018
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.

Sign in to comment.


Daniel Shub
Daniel Shub on 29 Sep 2011
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

Tags

Community Treasure Hunt

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

Start Hunting!