My User-defined Function is in red
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I followed the exact format to set up a user-defined function in the first line. Even my classmates' worked perfectly fine but mine. Can anyone show me why my function is in red?
1 commentaire
Stephen23
le 18 Mai 2024
@CHUN HIN KYLE: please click on the red underline/exclamation mark and show us the complete message that it shows.
Réponses (1)
VBBV
le 18 Mai 2024
Modifié(e) : VBBV
le 18 Mai 2024
>>[numout,unit] = MyUnitConverter(25,'cels2fahr')
Call your function as above from command window
6 commentaires
VBBV
le 19 Mai 2024
Modifié(e) : VBBV
le 19 Mai 2024
@CHUN HIN KYLE No, That code is to execute the function from command window (see below).
>>[numout,unit] = MyUnitConverter(numin,'cels2fahr')
The existing syntax of the line 1 in your file MyUnitConverter.m seems correct except that an additional space is present between output arguments. Normally it should work fine but try without any space as below
function [numout,unit] = MyUnitConverter(numin,convtype)
It is important to understand about the program purpose, (which is something provided to you from your instructor in your university). From the snapshot, it appears that the program takes two input arguments viz. numin and convtype The first argument is the number to be converted and the second argument is the conversion type specfifed by the user when calling the function from command window as shown earlier.
In order for the user to select an appropriate type of conversion, you can modify the program as below
function [numout,unit] = MyUnitConverter() % line 1
numin = input('Enter the input number : ')
disp('Select the conversion type ')
disp('1. Celsius to Fahrenheit')
disp('2. Fahrenheit to Celsius')
disp('3. Centimeters to Inches')
disp('4. Inches to Centimeters')
disp('5. Meters to Foot')
disp('6. Foot to Meters')
disp('7. Kilometers to Miles')
disp('8. Miles to Kilometers')
disp('9. Grams to Ounces')
disp('10. Ounces to Grams')
disp('11. Kilograms to Pounds')
disp('12. Pounds to Kilograms')
disp('13. Tonnes to Tons')
disp('14. Tons to Tonnes')
convtype = input('Selected type:', 's')
switch convtype
case '1'
% do something
case '2'
% do something
%...
end
end
Stephen23
le 19 Mai 2024
Modifié(e) : Stephen23
le 19 Mai 2024
"... except that an additional space is present between output arguments"
Those space characters are permitted (and always have been):
[X , Y, Z] = mytest()
function [ a , b , c] = mytest()
a = pi;
b = sqrt(2);
c = i;
end
Until the OP gives more detail (e.g. uploads the Mfile) we don't know what the problem is.
My guess is some special whitespace/control character before the FUNCTION keyword (which would also explain that odd alignment).
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!