First variation: fix the switch and case lines and the indexing into cName.
switch color(ind)
case 0
disp(cName{ind+1});
case 1
disp(cName{ind+2});
Second variation: dispense with the switch statement altogether.
color=[2,1,3,0,1,3,1,0,2]
cName ={'yellow','red','green','blue'};
for ind=1:length(color)
disp(cName{color(ind)+1})
end
Third variation: use a string array instead of a cell array of char vectors. This is the modern way to handle text in MATLAB.
color=[2,1,3,0,1,3,1,0,2]
cName = ["yellow", "red", "green", "blue"];
for ind=1:length(color)
disp(cName(color(ind)+1))
end
Best Answer