reading strings with if statements

1 次查看(过去 30 天)
KA
KA 2015-11-15
评论: KA 2015-11-15
Hi I have the following:
clc
clear
matrix=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
letter='da';
if letter(1)=='a'
pos1=matrix(1,1)+matrix(1,3);
elseif letter(1)=='b'
pos1=matrix(2,1)+matrix(2,3);
elseif letter(1)=='c'
pos1=matrix(3,1)+matrix(3,3);
elseif letter(1)=='d'
pos1=matrix(4,1)+matrix(4,3);
if letter(2)=='a'
pos2=matrix(1,1)+matrix(1,3);
elseif letter(2)=='b'
pos2=matrix(2,1)+matrix(2,3);
elseif letter(2)=='c'
pos2=matrix(3,1)+matrix(3,3);
elseif letter(2)=='d'
pos2=matrix(4,1)+matrix(4,3);
end
disp(matrix)
disp(pos1)
disp(pos2)
This cannot be evaluated by MATLAB any clues?

回答(1 个)

Geoff Hayes
Geoff Hayes 2015-11-15
Antoni - you are missing an end after the third elseif just prior to the next if
elseif letter(1)=='d'
pos1=matrix(4,1)+matrix(4,3);
end %<-----missing end
if letter(2)=='a'
You need to end the first if and elseif block before you begin a new one.

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by