Calculation from user input with blank cells

조회 수: 1 (최근 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 CenterFile Exchange에서 Gears에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by