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)
댓글 수: 0
채택된 답변
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!