Effacer les filtres
Effacer les filtres

How to call a own function in other mfile?

24 vues (au cours des 30 derniers jours)
Antonio
Antonio le 3 Juil 2024 à 9:21
Commenté : Mathieu NOE le 3 Juil 2024 à 10:38
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

Réponse acceptée

Mathieu NOE
Mathieu NOE le 3 Juil 2024 à 9:55
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)
z = 3
%% function(s) below this line %%%%%%%%%
function z = myfunc (x,y)
z = x+y;
end
  2 commentaires
Antonio
Antonio le 3 Juil 2024 à 10:19
Thanks
Mathieu NOE
Mathieu NOE le 3 Juil 2024 à 10:38
as always, my pleasure !

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 3 Juil 2024 à 9:50
Modifié(e) : John D'Errico le 3 Juil 2024 à 9:50
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
PATHTOOL Open Set Path dialog box to view and change search path PATHTOOL opens the MATLAB Set Path tool, which allows you to view, manipulate, and save the MATLAB search path. See also PATH, ADDPATH, RMPATH, SAVEPATH. Documentation for pathtool doc pathtool Folders named pathtool matlab/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.
  1 commentaire
Antonio
Antonio le 3 Juil 2024 à 10:00
I saved the function in the same path of the mfile.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by