Using cases with strings

Hi, Im trying to make the following code work and it just wont seem to do what I want
Astr = '123456789'
switch Astr
case strcmp(Astr([2:4,5:7]), '234567' )
x = 5
case strcmp(Astr([2:4,5:7]), '123567' )
x = 6
case strcmp(Astr([2:4,5:7]), '345567' )
x = 7
otherwise
x = 8
end
I think it has to do with the way Im defining the string values, it keeps outputting x = 8 (the otherwise case). Please let me know how to fix thanks!

 채택된 답변

Iain
Iain 2013년 6월 19일

0 개 추천

What you put after the case needs to be every valid option to enter that case, and NOT an operation.
switch Astr([2:4,5:7])
case {'234567', '765432'}
x = 5;
case '123567'
x = 6;
case '234567'
x = 7;
otherwise
x=8;
end

댓글 수: 1

JP
JP 2013년 6월 19일
What if my Astr is constantly changing length, so I want to check a different interval?
Say for case 1, I want to check the interval 2:4,5:7, but then in case 2 I want to check the interval 3:5,6:8?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Propagation and Channel Models에 대해 자세히 알아보기

태그

질문:

JP
2013년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by