필터 지우기
필터 지우기

warning about units of parameters

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by