Calculation from user input with blank cells

7 ビュー (過去 30 日間)
Brandon Page
Brandon Page 2015 年 3 月 19 日
コメント済み: Andrew Newell 2015 年 3 月 19 日
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 件)

Andrew Newell
Andrew Newell 2015 年 3 月 19 日
編集済み: Andrew Newell 2015 年 3 月 19 日
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 件のコメント
Brandon Page
Brandon Page 2015 年 3 月 19 日
Yes, If the user was to omit a gear then it would just be left out of the expression.
Thanks
Andrew Newell
Andrew Newell 2015 年 3 月 19 日
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));

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGears についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by