How do i read a string with a for loop.

I need to read a string with a for loop and evaluate it s characters by threes.
say i have some string input = aaaaabaacaadaae....
i need to cut this into 3 piece sections, then run them through a switch statement.
switch (string)
case(aaa) x=1 case(aab)=2
and so on, then terminate when i got to the end of the string?
i have the switch statement already i just need to figure out how to evalute the string witha for loop.

답변 (1개)

Iman Ansari
Iman Ansari 2013년 4월 12일

0 개 추천

Hi
input = 'aaaaabaacaadaae';
n=1;
for i=1:3:size(input,2)
New{n}=input(i:min(i+2,size(input,2)));
n=n+1;
end

댓글 수: 4

Josh
Josh 2013년 4월 12일
could you explain exactly what your steps here are? i understand everything except
New{n}=input(i:min(i+2,size(input,2)));
Iman Ansari
Iman Ansari 2013년 4월 12일
편집: Iman Ansari 2013년 4월 12일
input(i:i+2) is true, but if your input isn't multiple 3 this gave index error. and New is a cell array.
Josh
Josh 2013년 4월 12일
do you know how to do this without using an array? i havent learned those as of yet?
Iman Ansari
Iman Ansari 2013년 4월 12일
편집: Iman Ansari 2013년 4월 12일
With array it's easier to index it's contents:
New{3}
New{5}
But without cell array number of your variables increase:
x1=input(1:3)
x2=input(4:6)
x3=input(7:9)

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by