warning about units of parameters

2 ビュー (過去 30 日間)
matlab coder
matlab coder 2021 年 3 月 20 日
コメント済み: matlab coder 2021 年 3 月 20 日
ı 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"
matlab coder
matlab coder 2021 年 3 月 20 日
thank you

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by