warning about units of parameters
2 views (last 30 days)
Show older comments
matlab coder
on 20 Mar 2021
Commented: matlab coder
on 20 Mar 2021
ı 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 Comments
Accepted Answer
Walter Roberson
on 20 Mar 2021
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 Comments
Walter Roberson
on 20 Mar 2021
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
More Answers (0)
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!