How to create a user defined function that contain if else function. Can I create a user function from the line e line %user defined function
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
%menu for user to choose
disp('Convert between Celcious and Fahrenheit');
disp('Choose a number:');
disp('1.Celcious to Fahrenheit');
disp('2.Fahrenheit to Celcious');
%input
choice = input("\nInput:",'s');
while (strcmpi(choice, '1') == 0 && strcmpi(choice, '2') == 0)
disp('Choose number 1 or 2');
choice = input("\nInput: ",'s');
end
%user defined function
if (choice == '1')
cel = input("\nEnter temperature in Celcious to convert to Ferenheit:");
while (cel < 0)
disp('Enter a positive number, please');
cel = input("\nEnter temperature in Celcious to convert to Ferenheit:");
end
if (cel >= 0)
cel2fa = (cel*1.8) + 32;
fprintf('Coverted: %.2f Fahrenheit \n',cel2fa);
end
end
if (choice == '2')
fa = input('\nEnter temperature in Ferenheit to convert to Celcious:');
while (fa < 0)
disp('Enter a positive number, please');
fa = input("\nEnter temperature in Celcious to convert to Ferenheit:");
end
if (fa >= 0)
fa2cel = (fa-32)/1.8;
fprintf('Coverted: %.2f Celcious \n',fa2cel);
end
end
0 commentaires
Réponse acceptée
DGM
le 11 Août 2021
Modifié(e) : DGM
le 11 Août 2021
Well, you can certainly make it into a function:
clearvars; clc;
% the user is never told the meaning of what they're selecting
disp('Choose number 1 or 2');
choice = input("\nInput: ",'s');
while ~ismember(choice,{'1','2'})
choice = input("\nInput: ",'s');
end
myfunction(choice); % call the function
% ----------------------------------------------------------------
% it's a function now i guess
function myfunction(choice)
switch choice
case '1'
cel = input("\nEnter temperature in Celcious to convert to Ferenheit:");
% why restrict the functionality to positive numbers only?
% the math works for negatitve numbers too
while (cel < 0)
disp('Enter a positive number, please');
cel = input("\nEnter temperature in Celcious to convert to Ferenheit:");
end
% what is the utility of formatted text output dumped directly to console?
if (cel >= 0)
cel2fa = (cel*1.8) + 32;
fprintf('Coverted: %.2f Fahrenheit \n',cel2fa);
end
case '2'
fa = input('\nEnter temperature in Ferenheit to convert to Celcious:');
while (fa < 0)
disp('Enter a positive number, please');
fa = input("\nEnter temperature in Celcious to convert to Ferenheit:");
end
if (fa >= 0)
fa2cel = (fa-32)/1.8;
fprintf('Coverted: %.2f Celcious \n',fa2cel);
end
end
end
... but I don't know that it improves much.
I know this is probably just homework, and the structure is stipulated by the given assignment. This pattern of prompting the user interactively for every input is just bad design, and I don't know why instructors love teaching it. It has no programmatically-useful inputs or outputs, and the fact that it relies on direct human interaction for all I/O means it's optimized for being tedious and prone to human error.
If you wanted a more practical function that did temperature conversions, you'd consider efficiency and flexibility of both the code and the usage. Where to start? The function only needs one numeric input and something that tells it what conversion to make. Decide on a convenient syntax and go. Get all the inputs first, convert the number, and prepare the outputs.
function varargout = tempconverter(tempin,convstring)
% TEMPOUT = TEMPCONVERTER(TEMPIN,CONVERSIONTYPE)
% blah blah blah this describes the function behavior and usage
%
% TEMPIN is a number (scalar or array)
% CONVERSIONTYPE is one of the following:
% 'ctof' for Celsius to Farenheit
% 'ftoc' for Farenheit to Celsius
%
% If called with scalar input and no output argument,
% a textual description of the conversion will be printed.
%
% See also: convtemp
switch lower(convstring)
case 'ftoc'
tempout = (tempin-32)*5/9;
case 'ctof'
tempout = 32 + (tempin*9/5);
otherwise
error('TEMPCONVERTER: invalid conversion type %s',convstring)
end
if nargout == 0 && isscalar(tempin)
switch lower(convstring)
case 'ftoc'
fprintf('%.2f°F is equal to %.2f°C\n',tempin,tempout)
case 'ctof'
fprintf('%.2f°C is equal to %.2f°F\n',tempin,tempout)
end
else
varargout{1} = tempout;
end
The above function will provide conversion for scalars or arrays, and if called with no outputs, it will do pretty formatted output just for reading.
tempconverter(200,'ftoc')
T = tempconverter([100 25 0 -40],'ctof')
The comment block at the top of the function is the help synopsis and describes the usage so that the user knows what they're doing instead of being asked to blindly pick a number. Writing this first may help you decide what syntax will be convenient.
help tempconverter
If you wanted to add a new conversion to that example, all you'd need is one new line in the synopsis, and one new case in each of the two switch-case structures -- 5 lines. It could definitely be simpler than that, but it's a start toward reducing repeated code.
That said, I'm sure that this example doesn't directly satisfy the requirements of your assignment, but you're free to use it directly or conceptually to make something that does.
2 commentaires
DGM
le 12 Août 2021
I don't know what you mean about listing the input. Was that a requirement of the assignment?
If you're asking about the lack of input() calls in the second example I gave, it doesn't use interactive prompts at all. It would be called directly with the desired parameters as shown.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!