MATLAB Answers

Error when If statement is not true

조회 수: 7(최근 30일)
Kateri Kaminski
Kateri Kaminski 2018년 10월 31일
댓글: Kateri Kaminski 2018년 11월 5일
Hello,
I have a user supplying a 'Yes' or 'No' input to a question. I want to use the response to determine if plots are created or not. If I select 'Yes', the code runs fine. However, if I select 'No', I get this error: Error in PostProcessing_filetest (line 118) , if answer == 'Yes'. Why won't Matlab go to the else statement if the if-statement is not true? Can someone help point out my error? Below is a portion of my code. The variables in the if-portion of the code are defined before-hand. Thank you!
....
quest = 'Do you want another set of maps?';
answer = questdlg(quest);
if answer == 'Yes'
strm1a = 'What is the map height (pixels)?';
strm2a = 'What is the map width (pixels)?';
strm3a = 'What is the min Frequency?';
strm4a = 'What is the max Frequency?';
strm5a = 'What is the min x parameter?';
strm6a = 'What is the max x parameter?';
strmat = {strm1a,strm2a,strm3a,strm4a,strm5a,strm6a};
xminstrg = round(min(min(min(xparm))));
xmaxstrg = round(max(max(max(xparm))));
default = {'300','500','0','200','0','1000'};
inputmapa = inputdlg(strmat,'New Map Parameters',[1 50],default);
h = str2double(inputmapa{1});
w = str2double(inputmapa{2});
mapFmin = str2double(inputmapa{3});
mapFmax = str2double(inputmapa{4});
mapxmin = str2double(inputmapa{5});
mapxmax = str2double(inputmapa{6});
i = 1;
for i = 1: numfiles
figure(fignum)
fignum = fignum + 1;
hold on
axis([mapxmin mapxmax mapFmin mapFmax])
himage = imagesc(xparm(:,:,i),Farray,Amp(:,:,i));
colormap;
hAxes = himage.Parent;
ampmax(i) = max(max(Amp(:,:,i)));
hAxes.CLim = [s,ampmax(i)/10];
xlabel(strcat(namexparm,'(', unitsxparm, ')'));
ylabel("Frequency(Hz)");
title(strcat("Waterfall map for: Test ", testname , "-", fn(i)))
colorbar;
truesize([h w]);
hold off
end
else
disp("no more plots");
end
.....

채택된 답변

Amy Haskins
Amy Haskins 2018년 11월 5일
The problem is with using == on character vectors. Each character is treated as a separate element and compared individually. This will error when the lengths of the strings you are trying to compare doesn't match.
>> 'no' == 'yes'
Matrix dimensions must agree.
>> 'yes' == 'yes'
ans =
1×3 logical array
1 1 1
There are 2 easy solutions. One is to use strcmp. The other is to use the string data type (introduced in R2016b) which will behave more like you were expecting.
>> strcmp('no','yes')
ans =
logical
0
>> "no" == "yes" % String scalars
ans =
logical
0
  댓글 수: 1
Kateri Kaminski
Kateri Kaminski 2018년 11월 5일
Oh this makes sense. Thanks!

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by