matlab外部.m文件函数返回

时间:2018-02-09 01:18:36

标签: matlab

我有Main.m和External.m

我想从Main.m

调用External.m中的函数

Main.m代码:

clc
External
disp('Press any key to go to advance')
pause

External.m代码:

function f=EPD470_ObermeierC_Lesson05_MFile
    x=0:4:40
end

它确实正确显示了我想要的值:

x =
 0     4     8    12    16    20    24    28    32    36    40

然后抛出错误:

  

输出参数" f" (也许是其他人)在通话期间未分配   " EPD470_ObermeierC_Lesson05_MFile"

编辑:

我正在尝试显示由External.m

返回的Main.m中的值

更改

Main.m代码:

clc
y=External
disp(y)
pause

(编辑反映拼写错误) External.m代码:

function f=External
    f=0:4:40
end

它没有显示我想要的值。

1 个答案:

答案 0 :(得分:0)

欢迎来到MATLAB!希望你喜欢这门语言。

下面您将找到External.m和Main.m的更新代码(Main.m的两个选项),其中包含大量注释,解释了为何建议更改。

External.m:

% generally it's best practice to name the function the same as in the
% filename. This won't impact the running of the code
function f = External

% change the main line of the function to assign the value to the correct
% variable name. Also, add a semicolon to the end of this line to suppress
% output (when a variable is assigned in MATLAB in a line that does not
% end in a semicolon, the MATLAB console displays that variable)
f = 0:4:40;

% the end command isn't necessary if just one function is defined in the
% file. Again, it won't impact the running of the code, but for simplicity
% it may be preferable to omit

Main.m - 选项1:

% unchanged
clc

% assign the output of External to a variable. Note that the name of the
% variable used here may be the same as used inside the function, or it may
% not be the same. Note also that a semicolon is used to suppress output
x = External;

% explicitly display the variable
disp(x);

% unchanged
disp('Press any key to go to advance')
pause

Main.m - 选项2:

% unchanged
clc

% assign the output of External to a variable. Note that the name of the
% variable used here may be the same as used inside the function, or it may
% not be the same. The absence of a semicolon on a line where a variable
% is assigned means that the MATLAB console will display that variable
x = External

% unchanged
disp('Press any key to go to advance')
pause