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.

 Respuesta aceptada

Pedro Villena
Pedro Villena el 31 de Oct. de 2012
Editada: Pedro Villena el 31 de Oct. de 2012
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 comentarios

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 el 31 de Oct. de 2012
I should have hit refresh, I guess ;-).

Iniciar sesión para comentar.

Más respuestas (4)

Sean de Wolski
Sean de Wolski el 31 de Oct. de 2012
Pedro's code with numel as the size check:
numel(str)==5
manoj saini
manoj saini el 31 de Oct. de 2012

0 votos

>>a='string'; >>n=length(a) so n will return length of a string
Ze Kai Ng
Ze Kai Ng el 31 de Oct. de 2012

0 votos

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

0 votos

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.

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by