Calculation from user input with blank cells

3 views (last 30 days)
I am working on a project to analyze simple gear trains and have created a dialogue box for the user to input the number of teeth on each gear. The values are used in an equation to calculate the gear train ratio and I am having trouble figuring out how to calculate the ratio if some cells are left blank due to not having 10 gears.
My code so far:
prompt = {'Gear 1:','Gear 2:','Gear 3:','Gear 4:','Gear 5:','Gear 6:',...
'Gear 7:','Gear 8:','Gear 9:','Gear 10:'};
title = 'Number of teeth per gear';
answer = inputdlg(prompt,title);
N1 = str2num(answer{1});
N2 = str2num(answer{2});
N10 = str2num(answer{10});
Mo = (N1*N3*N5*N7*N9)/(N2*N4*N6*N8*N10);

Answers (1)

Andrew Newell
Andrew Newell on 19 Mar 2015
Edited: Andrew Newell on 19 Mar 2015
The answer depends on how you would calculate the ratio Mo for fewer gears. If, say, the user omits Gear 3, do you want to simply leave it out, as in this expression?
Mo = (N1*N5*N7*N9)/(N2*N4*N6*N8*N10);
Supposing you do, you could set a default of 1 for the input:
num_lines = 1;
default = repmat({'1'},1,10);
answer = inputdlg(prompt,title,num_lines,default);
  2 Comments
Brandon Page
Brandon Page on 19 Mar 2015
Yes, If the user was to omit a gear then it would just be left out of the expression.
Thanks
Andrew Newell
Andrew Newell on 19 Mar 2015
An alternative, if you don't want to see those default 1's in the dialog boxes, is to process the answers like this:
N = ones(size(answer));
for ii=1:length(answer)
if ~isempty(answer{ii})
N(ii) = str2num(answer{ii});
end
end
And then you can calculate your expression:
M0 = prod(N(1:2:end))/prod(N(2:2:end));

Sign in to comment.

Categories

Find more on Gears 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!