Arrays have incompatible sizes for this operation problem in if statement

15 views (last 30 days)
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');
Error using readtable
Unable to find or open 'stats.csv'. Check the path and filename or file permissions.
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

Accepted Answer

Steven Lord
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)
ans = logical
1
string(a) == string(b)
ans = logical
0
strcmp(a, p)
ans = logical
0
matches(a, p)
ans = logical
0
startsWith(p, a)
ans = logical
1
x = 'banana';
switch x
case a
disp('Apple!')
case b
disp('Banana!')
case p
disp('Apple Pie!')
otherwise
disp('I don''t know!')
end
Banana!
% This won't work
a == b
Arrays have incompatible sizes for this operation.
% nor would this work, for the same reason
(1:5) == (1:10)

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!