warning about units of parameters

3 次查看(过去 30 天)
ı have started to create such a code shown below. but ı need to do something whıch ı couldnt until now. what can ı add on my code to make it unit sensitive? because ı want to use ''meter'' and what if user enters milimeter the program should warn him to fix it and then continue. thanks in advance...
l=input('enter a value for length:'); %bearing length (m)
d=input('enter a value for diameter:') ; %journal diameter (m)
r=d/2; %radius (m)
c=input('enter a radial clearance:'); % radial clearance (m)
T1=input('assign an inlet temperature:'); %inlet temperature (celcius)
delta_T=input('enter temperature rise value:'); %assumed temperature rise (celcius)
W=input('enter a radial load on journal:'); %radial load on journal (N)

采纳的回答

Walter Roberson
Walter Roberson 2021-3-20
You will want to use the 's' option of input() to receive the user response as text, and pick out any user units as text, check the unit, and if the unit is okay, convert the numeric part.
If you want to do units conversion you might find it easiest to use Symbolic Toolbox symunit https://www.mathworks.com/help/symbolic/symunit.html
  3 个评论
Walter Roberson
Walter Roberson 2021-3-20
known_units = {'m', 'mm', 'kg', 'C', 'N', 'Hz'};
for K = 1 : 5
try
if ispc()
l = input('enter a value for length: ', 's'); %bearing length (m)
else
if rand < 1/3
bads = {'ask Bob', '3.141 F'};
l = bads{randi(length(bads))};
else
l = sprintf('%e%s', randn()*10, known_units{randi(length(known_units))});
end
end
fprintf('Input was: "%s"\n', l);
L = l;
l_unit = '';
l_unit_pos = regexp(l, '\D+$', 'start');
if ~isempty(l_unit_pos)
l_unit = strtrim(l(l_unit_pos:end));
l = l(1:l_unit_pos-1);
if ~ismember(l_unit, known_units)
error('Unknown unit found in entry "%s"', L);
end
end
lnum = str2double(l);
if isnan(lnum)
error('Bad number found in entry "%s"', L);
end
fprintf('number was %g unit was "%s"\n', lnum, l_unit);
catch ME
disp(ME.message)
end
end
Input was: "-7.602150e+00kg"
number was -7.60215 unit was "kg"
Input was: "ask Bob"
Unknown unit found in entry "ask Bob"
Input was: "3.141 F"
Unknown unit found in entry "3.141 F"
Input was: "1.528264e+01mm"
number was 15.2826 unit was "mm"
Input was: "-1.041360e+00N"
number was -1.04136 unit was "N"

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by