Filter löschen
Filter löschen

Find a length of string/array without length function

3 Ansichten (letzte 30 Tage)
KKR
KKR am 15 Feb. 2012
Bearbeitet: Matt J am 13 Okt. 2013
Hi All,
Hope everyone is doing great. I have minor question, I am trying to write a simple function to find a length of string/array, of course without using length function of MATLAB.
I am trying to find if there is any condition related to error message index bound, then I can use it in loop. Any other ideas are more than welcome.
Thank you in advance.

Akzeptierte Antwort

James Tursa
James Tursa am 15 Feb. 2012
I have no idea what the point of this question is, but do you mean something like this?
try
k = 1;
while( true )
str(k);
k = k + 1;
end
catch
strlength = k - 1;
end
  1 Kommentar
KKR
KKR am 16 Feb. 2012
Hi James,
I have been given an exercise (or else I would never think of something like this...).
This is what I tried:
% i=1;
% while ~isempty(strop(i))==1
% i = i+1;
% end
% ln = i-1;
The problem is it gets index bound error. I am not sure how is it different than what you said. Please let me know, if you see where am I missing out?
Thank you for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sean de Wolski
Sean de Wolski am 15 Feb. 2012
str = 'hello world!';
max(size(str))
  2 Kommentare
KKR
KKR am 15 Feb. 2012
Sean,
This works perfect. But, I dont think I can even use that. I would like to write really basic code, with looping to find the number of elements.
Thank for your help, nonetheless.
Kaushal
Sean de Wolski
Sean de Wolski am 15 Feb. 2012
Then write it!

Melden Sie sich an, um zu kommentieren.


bym
bym am 16 Feb. 2012
a variant on your code
str = 'hello world';
c =0;
while ~isempty(str)
c = c+1;
str(1)=[];
end
c

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by