Clear Filters
Clear Filters

reading strings with if statements

1 view (last 30 days)
KA
KA on 15 Nov 2015
Commented: KA on 15 Nov 2015
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?

Answers (1)

Geoff Hayes
Geoff Hayes on 15 Nov 2015
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.

Community Treasure Hunt

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

Start Hunting!