How to display functions with 2 outputs
3 次查看(过去 30 天)
显示 更早的评论
I am confused on how to display functions with 2 outputs, for my unit converter I need to display the output and the unt of that output an example would be 32 F but I cant seem to figure it out.
Basically it has to ensure the function returns both the converted value (numout) along with the unit relating to this output number (unit).
a = input('Please enter a number ');
disp("Select what conversion you want to do:")
disp('1. Celsius to Fahrenhiet')
convtype = input("Please enter the conversion type: ");
switch convtype
case 1
conversion = 'cels2fahr';
answer = myunitconv(a, conversion);
disp([num2str(answer)]);
end
myunitconv(a, conversion)
function [numout, unit] = myunitconv(numin, convtype)
if strcmp(convtype,'cels2fahr')
numout = (numin*(9/5))+32;
unit = ;
else
error('Invalid conversion type')
end
end
0 个评论
采纳的回答
VBBV
2024-4-16
a = 212% input('Please enter a number ');
disp("Select what conversion you want to do:");
disp('1. Celsius to Fahrenhiet')
convtype = 1 %input("Please enter the conversion type: ");
switch convtype
case 1
conversion = 'cels2fahr';
[answer unit] = myunitconv(a, conversion);
disp([num2str(answer) unit ]);
end
function [numout, unit] = myunitconv(numin, conversion)
if strcmp(conversion,'cels2fahr')
numout = (numin*(9/5))+32;
unit = 'F';
else
error('Invalid conversion type')
end
end
2 个评论
VBBV
2024-4-16
you can add more cases and correspnding units (inside the function) to display the answer and unit as output
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!