Calculation from user input with blank cells

2 visualizaciones (últimos 30 días)
Brandon Page
Brandon Page el 19 de Mzo. de 2015
Comentada: Andrew Newell el 19 de Mzo. de 2015
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);

Respuestas (1)

Andrew Newell
Andrew Newell el 19 de Mzo. de 2015
Editada: Andrew Newell el 19 de Mzo. de 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 comentarios
Brandon Page
Brandon Page el 19 de Mzo. de 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 el 19 de Mzo. de 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));

Iniciar sesión para comentar.

Categorías

Más información sobre Gears en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by