How to check the numbers of characters of a string?

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
Pedro Villena 2012년 10월 31일
편집: Pedro Villena 2012년 10월 31일

1 개 추천

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

댓글 수: 3

This won't work, consider:
num2str([17;10],'%05i')
ans =
00017
00010
length(ans)
Better to use NUMEL, for generality.
S = ['ertyu';'poiuy';'lkjhg';'nbvcx']
length(S)
numel(S)
Matt Fig
Matt Fig 2012년 10월 31일
I should have hit refresh, I guess ;-).

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

추가 답변 (4개)

Sean de Wolski
Sean de Wolski 2012년 10월 31일

1 개 추천

Pedro's code with numel as the size check:
numel(str)==5
manoj saini
manoj saini 2012년 10월 31일

0 개 추천

>>a='string'; >>n=length(a) so n will return length of a string
Ze Kai Ng
Ze Kai Ng 2012년 10월 31일

0 개 추천

I converted my string into vector form and cannot count using length(str).
Ze Kai Ng
Ze Kai Ng 2012년 10월 31일

0 개 추천

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.

카테고리

도움말 센터 및 File Exchange에서 Simulink Check에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by