MATLAB: EDIT:(Getting a Different Error Now: ” Index exceeds matrix dimensions”.) >>User Inputed Word–> Yields a corresponding number.

stringsuserinput

>PREVIOUS:
>Hello, this is a small part of a larger code I am writing that is supposed to give a total "value" of pieces of scrap >metal at a junk yard. The other parts are fairly straightforward, and I have had no trouble figuring them out >(involved basic statements), except for the final part.
>Anyway, A user will input a range of numbers describing things such as the "length, width, mass, etc", and then a >"value" of the piece of >metal is determined by adding up the sub scores. These numbers range from a grade of 1-5.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Thanks for the help earlier guys, but now I am getting a new error.
My new code: (the x1, x2, x3 ect are just arbitrary variables to store the sub-scores)
cond = input('Condition');
switch cond
case 1
x1=0;
case 2
x1=5;
case 3
x1=10;
case 4
x1=15;
case 5
x1=20;
otherwise
x1=0;
end
color = input('Color');
switch lower(color)
case {'blue' 'white'}
x2 = 12;
case 'red'
x2 = 20;
case 'pink'
x2 = 2;
case 'black'
x2 = 15;
otherwise
x2 = 0;
end
mass= input('Mass');
switch mass
case 1
x3 = 8;
case 2
x3= 16;
case 3
x3=16;
case 4
x3=20;
case 5
x3=20;
otherwise
x3=0;
end
length= input('Length: ');
switch length
case 1
x4=8;
case 2
x4=8;
case 3
x4=19;
case 4
x4=19;
case 5
x4=19;
otherwise
x4=0;
end
width= input('width: ');
switch width
case 1
x5=6;
case 2
x5=13;
case 3
x5=13;
case 4
x5=18;
case 5
x5=18;
otherwise
x5=0;
end
points=(x1)+(x2)+(x3)+(x4)+(x5);
disp(['The value of that piece of scrap is ', num2str(points), ' points'])
The error "Index exceeds matrix dimensions" is occurring at every "input" line.

Best Answer

Spencer: Try it this way:
clc; % Clear command window.
format compact; % Remove unnecessary blank lines from output.
workspace; % Display the workspace panel.
defaultValue = 1;
titleBar = 'Enter a value';
caUserInput = inputdlg('Enter the condition ', titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.




cond = round(str2double(cell2mat(caUserInput)));
switch cond
case 1
x1=0;
case 2
x1=5;
case 3
x1=10;
case 4
x1=15;
case 5
x1=20;
otherwise
x1=0;
end
caUserInput = inputdlg('Enter the color ', titleBar, 1, {num2str(defaultValue)});
color = round(str2double(cell2mat(caUserInput)));
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
switch lower(color)
case {'blue' 'white'}
x2 = 12;
case 'red'
x2 = 20;
case 'pink'
x2 = 2;
case 'black'
x2 = 15;
otherwise
x2 = 0;
end
caUserInput = inputdlg('Enter the mass ', titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
mass = round(str2double(cell2mat(caUserInput)));
switch mass
case 1
x3 = 8;
case 2
x3= 16;
case 3
x3=16;
case 4
x3=20;
case 5
x3=20;
otherwise
x3=0;
end
caUserInput = inputdlg('Enter the length ', titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
length = round(str2double(cell2mat(caUserInput)));
switch length
case 1
x4=8;
case 2
x4=8;
case 3
x4=19;
case 4
x4=19;
case 5
x4=19;
otherwise
x4=0;
end
caUserInput = inputdlg('Enter the width ', titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
width = round(str2double(cell2mat(caUserInput)));
switch width
case 1
x5=6;
case 2
x5=13;
case 3
x5=13;
case 4
x5=18;
case 5
x5=18;
otherwise
x5=0;
end
points=(x1)+(x2)+(x3)+(x4)+(x5)
fprintf('The value of that piece of scrap is %d points.\n', points)