필터 지우기
필터 지우기

if statment. why do i receive following statment: Operands to the || and && operators must be convertible to logical scalar values.

조회 수: 1 (최근 30일)
Hi all plz help me.I don't understand why following code is not working..
choice=input('enter something:','s');
if str2num(choice)==1 lower(choice) == 'load the data'
disp('loading the data')
end
when i run the code matlab says following:
Operands to the and && operators must be convertible to logical scalar values.
thx..

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 6월 21일
편집: Geoff Hayes 2014년 6월 21일
The error is occurring when the user enters a choice that is non-numeric and the conversion from str2num is empty (i.e. []) which is NOT a logical scalar value operand to the (missing?) or () operator. (In the Command Window type str2num('hello') and observe the result.)
To get around this, you could convert the string to a number and check that it is not empty and equal to one, OR compare the string
choiceStr = input('enter something:','s');
choiceNum = str2num(choiceStr);
if (~isempty(choiceNum) && choiceNum==1) || lower(choiceStr) == 'load the data'
disp('loading the data');
end
Even the above is not quite correct. When comparing strings it is preferable to use the strcmp or strcmpi functions. Since you are not interested case (because of the lower) then just use strcmpi
choiceStr = input('enter something:','s');
choiceNum = str2num(choiceStr);
if (~isempty(choiceNum) && choiceNum==1) || strcmpi(choiceStr,'load the data')==1
disp('loading the data');
end
Try the above and see what happens!

추가 답변 (0개)

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by