My User-defined Function is in red
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Stephen23
2024년 5월 18일
@CHUN HIN KYLE: please click on the red underline/exclamation mark and show us the complete message that it shows.
답변 (1개)
VBBV
2024년 5월 18일
편집: VBBV
2024년 5월 18일
>>[numout,unit] = MyUnitConverter(25,'cels2fahr')
Call your function as above from command window
댓글 수: 6
VBBV
2024년 5월 19일
편집: VBBV
2024년 5월 19일
@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
2024년 5월 19일
편집: Stephen23
2024년 5월 19일
"... 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).
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!