필터 지우기
필터 지우기

Simple if statement question

조회 수: 4 (최근 30일)
JP
JP 2013년 6월 19일
Hi, just getting the hang of the basics of MATLAB and had a quick easy question. I have a variable defined as a string and I want to use an if statement on two parts of the string seperately.
EX.
Astr = '123456789'
if Astr(2:4,5:7) == '234','567'
x = 5
end
Of course I am hoping x = 5 would appear when I run this, but it gets an error on the 'if' line. Let me know how to accomplish this!

채택된 답변

the cyclist
the cyclist 2013년 6월 19일
편집: the cyclist 2013년 6월 19일
You are pretty much inventing your own syntax here, and MATLAB won't understand that. :-)
One way to perform this test is
Astr = '123456789'
if strcmp(Astr(2:4),'234') && strcmp(Astr(5:7),'567')
x = 5;
end
The string comparison command that I used here is better than using "==" (which is more appropriate for numeric comparisons).
  댓글 수: 1
lvn
lvn 2013년 6월 19일
Or shorter, use strcmp(Astr([2:4,5:7]), '234567' )

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

추가 답변 (1개)

JP
JP 2013년 6월 19일
Thanks very much! Yes I figured I was inventing a few things when doing that haha

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by