Calculation from user input with blank cells

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);

回答(1 个)

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 个评论

Yes, If the user was to omit a gear then it would just be left out of the expression.
Thanks
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));

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Gears 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by