My User-defined Function is in red

조회 수: 2 (최근 30일)
CHUN HIN KYLE
CHUN HIN KYLE 2024년 5월 18일
편집: Stephen23 2024년 5월 19일
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
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
VBBV 2024년 5월 18일
편집: VBBV 2024년 5월 18일
>>[numout,unit] = MyUnitConverter(25,'cels2fahr')

Call your function as above from command window

  댓글 수: 6
VBBV
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
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()
X = 3.1416
Y = 1.4142
Z = 0.0000 + 1.0000i
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).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by