Arrays have incompatible sizes for this operation problem in if statement
15 views (last 30 days)
Show older comments
I'm coding a project for my first year uni course and when proposing an if statement i keep getting the same error and my code is so simple i cant find anything that answers my question online. when running this code i keep getting the error: Arrays have incompatible sizes for this operation.
Can anyone see what the problem is? When i remove the elseif, and just have the one 'if' statement the program runs and gives the correct data.
% use inp variable to determine what dataset to load
playerstats = readtable('stats.csv');
players = playerstats{1:64,'Player'};
[indx,tf] = listdlg('ListString',players,'Name','What player would you like to see stats for?');
% what stat does the user want to see
statview = input('What stat would you like to see from this player? (kd, kills, kills per life, damage per life, team kills, game points)','s');
%convert to matrix
ps = table2cell(playerstats);
psm = cell2mat(ps(:,4:10));
% determining stat option
if statview == 'kd'
disp(psm(indx,1))
elseif statview == 'kills'
disp(psm(indx,2))
end
0 Comments
Accepted Answer
Steven Lord
on 12 Dec 2022
Don't use == for comparing char vectors. If the char vectors you're comparing don't have the same number of characters that won't work. Instead use string arrays, strcmp, matches, startsWith, or (if you have a fixed set of possibilities) switch / case.
a = 'apple';
b = 'banana';
p = 'apple pie';
string(a) == string(a)
string(a) == string(b)
strcmp(a, p)
matches(a, p)
startsWith(p, a)
x = 'banana';
switch x
case a
disp('Apple!')
case b
disp('Banana!')
case p
disp('Apple Pie!')
otherwise
disp('I don''t know!')
end
% This won't work
a == b
% nor would this work, for the same reason
(1:5) == (1:10)
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!