How to call a own function in other mfile?
1 次查看(过去 30 天)
显示 更早的评论
Good morning,
I create my own function
function z = myfunc (x,y)
z = x+y;
end
I have to use it into a mfile how can do it ?
Regards
0 个评论
采纳的回答
Mathieu NOE
2024-7-3
hello
you simply need to have your function(s) located at the end of your script (mfile)
% example code
%% main code
x = 1;
y = 2;
z = myfunc (x,y)
%% function(s) below this line %%%%%%%%%
function z = myfunc (x,y)
z = x+y;
end
更多回答(1 个)
John D'Errico
2024-7-3
编辑:John D'Errico
2024-7-3
Did you save it on your search path? (Do you know what your search path is?) Spend some time reading the help for tools like addpath, savepath, pathtool, etc.
help pathtool
Do NOT try to save that file into the directories supplied with MATLAB. DO NOT DO THAT. Use your own directory.
Once that is done, now just call it like you would any other function,
x = 12;
y = 1:3;
z = myfunc(x,y)
And you can use that function in any other function you write now too.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!