MATLAB Answers

Error when If statement is not true

11 views (last 30 days)
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
fignum = fignum + 1;
hold on
axis([mapxmin mapxmax mapFmin mapFmax])
himage = imagesc(xparm(:,:,i),Farray,Amp(:,:,i));
hAxes = himage.Parent;
ampmax(i) = max(max(Amp(:,:,i)));
hAxes.CLim = [s,ampmax(i)/10];
xlabel(strcat(namexparm,'(', unitsxparm, ')'));
title(strcat("Waterfall map for: Test ", testname , "-", fn(i)))
truesize([h w]);
hold off
disp("no more plots");

Accepted Answer

Amy Haskins
Amy Haskins on 5 Nov 2018
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 =
>> "no" == "yes" % String scalars
ans =

More Answers (0)

Community Treasure Hunt

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

Start Hunting!