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

1 view (last 30 days)
>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.

Accepted Answer

Image Analyst
Image Analyst on 13 Apr 2012
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)
  2 Comments
Image Analyst
Image Analyst on 13 Apr 2012
Notem these:
caUserInput = inputdlg('Enter the width ', titleBar, 1, {num2str(defaultValue)});
is supposed to be all on one single line (in case it doesn't show up that way in your browser).

Sign in to comment.

More Answers (3)

Geoff
Geoff on 13 Apr 2012
Nah... Try this:
colors = {'Blue', 'Red', 'Pink', 'Black', 'White'};
values = [12 20 2 15 12];
You can search for the index of your colour like this:
col = find( cellfun(@(c) strcmpi(c, color), colors) );
Or, the more clunky approach is to use a switch statement...
switch lower(color)
case 'blue'
value = 12;
case 'red'
value = 20;
.....
end
Hope that wasn't too much hinting.
  3 Comments
Spencer
Spencer on 13 Apr 2012
Oh! Okay, that makes WAAAAY more sense then what I was attempting to grind out. Thank you to you both!
Geoff
Geoff on 13 Apr 2012
I mean more clunky because if you simply want to map a string to a value, a switch statement can become very cluttered. Plus, using arrays means you can generalise the solution into a function (which could inform the user what the available options are if they enter an invalid value). It reduces the possibility of having to update your code in more than one place if you want to make a change, and you can use the same function for any string->value input.

Sign in to comment.


Image Analyst
Image Analyst on 13 Apr 2012
Why are you messing with strings and input()? Why not use menu(), like this:
%Blue=12
%Red=20
%Pink=2
%Black=15
%White=12
button = menu('Pick a color', 'Blue', 'Red', 'Pink', 'Black', 'White')
switch button
case 1
output = 12
case 2
output = 20
case 3
output = 2;
case 4
output = 15
otherwise
output = 12
end
message = sprintf('The value is %d', output);
msgbox(message);

Spencer
Spencer on 13 Apr 2012
Hi, after taking into consideration the tips you both have given me so far, I sort of simplified the code I had, from long if then statements to switch statements.(As shown above)
How ever I am now getting an error "Index exceeds matrix dimensions"
It is occurring at every "input" line. Am I missing some crucial detail/ making a critical mistake that you can see?

Community Treasure Hunt

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

Start Hunting!