## How to check the numbers of characters of a string?

on 31 Oct 2012

### Pedro Villena (view profile)

e.g I have a string, abcde. And I need a checker to check whether the string has 5 characters before executing a command.

### Pedro Villena (view profile)

on 31 Oct 2012
Edited by Pedro Villena

### Pedro Villena (view profile)

on 31 Oct 2012

str = num2str([17;10],'%05i')
if length(str(1,:))==5,
end
or
str = num2str([17;10],'%05i')
if numel(str(1,:))==5,
end

Sean de Wolski

### Sean de Wolski (view profile)

on 31 Oct 2012
This won't work, consider:
num2str([17;10],'%05i')
ans =
00017
00010
length(ans)
Matt Fig

### Matt Fig (view profile)

on 31 Oct 2012
Better to use NUMEL, for generality.
S = ['ertyu';'poiuy';'lkjhg';'nbvcx']
length(S)
numel(S)
Matt Fig

### Matt Fig (view profile)

on 31 Oct 2012
I should have hit refresh, I guess ;-).

### Sean de Wolski (view profile)

on 31 Oct 2012

Pedro's code with numel as the size check:
numel(str)==5

### manoj saini (view profile)

on 31 Oct 2012

>>a='string'; >>n=length(a) so n will return length of a string

### Ze Kai Ng (view profile)

on 31 Oct 2012

I converted my string into vector form and cannot count using length(str).