How to check the numbers of characters of a string?

23 views (last 30 days)
e.g I have a string, abcde. And I need a checker to check whether the string has 5 characters before executing a command.

Accepted Answer

Pedro Villena
Pedro Villena on 31 Oct 2012
Edited: Pedro Villena on 31 Oct 2012
str = num2str([17;10],'%05i')
if length(str(1,:))==5,
%%%here is your command
str = num2str([17;10],'%05i')
if numel(str(1,:))==5,
%%%here is your command

Sign in to comment.

More Answers (4)

Sean de Wolski
Sean de Wolski on 31 Oct 2012
Pedro's code with numel as the size check:

manoj saini
manoj saini on 31 Oct 2012
>>a='string'; >>n=length(a) so n will return length of a string

Ze Kai Ng
Ze Kai Ng on 31 Oct 2012
I converted my string into vector form and cannot count using length(str).

Ze Kai Ng
Ze Kai Ng on 31 Oct 2012
Thank you everybody! I figured it out. I need to char(vec(pos)) first to convert to string then I can compare with the above methods.


Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!