概述
我的答案中提供的所有信息都可以在
Function Basics MATHWORKS找到.
如何在MATLAB中创建一个函数?您使用以下模板.此外,函数的名称和文件的名称应该类似.
% ------------------- newFunc.m--------------------
function [out1,out2] = newFunc(in1,in2)
out1 = in1 + in2;
out2 = in1 - in2;
end
%--------------------------------------------------
要使用多个函数,可以将它们应用于单独的m文件或使用嵌套/本地结构:
单独的m文件:
在此结构中,您将每个函数放在一个单独的文件中,然后在主文件中按名称调用它们:
%--------- main.m ----------------
% considering that you have written two functions calling `func1.m` and `func2.m`
[y1] = func1(x1,x2);
[y2] = func2(x1,y1);
% -------------------------------
地方职能:
在此结构中,您只有一个m文件,在此文件中可以定义多个函数,例如:
% ------ main.m----------------
function [y]=main(x)
disp('call the local function');
y=local1(x)
end
function [y1]=local1(x1)
y1=x1*2;
end
%---------------------------------------
嵌套函数:
在此结构中,函数可以包含在另一个函数中,例如:
%------------------ parent.m -------------------
function parent
disp('This is the parent function')
nestedfx
function nestedfx
disp('This is the nested function')
end
end
% -------------------------------------------
您无法从m文件外部调用嵌套函数.为此,您必须为每个函数使用单独的m文件,或使用类结构.
最后
以上就是平淡墨镜为你收集整理的matlab在脚本中用函数,如何从另一个脚本“调用”Matlab函数的全部内容,希望文章能够帮你解决matlab在脚本中用函数,如何从另一个脚本“调用”Matlab函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复