How to call functions from another m file

I have two scripts. In first script I have some functions.
script1.m:
function res = func1(a)
res = a * 5;
end
function res = func2(x)
res = x .^ 2;
end
In second script I call these functions. How to include script1.m in second script and call functions from script1.m?

 Respuesta aceptada

Adam
Adam el 9 de Mzo. de 2017

7 votos

You can't if the functions are defined as local functions in the script1 file.
Just put the functions in their own separate file (of the same name as the function and on your path) if they are being used by both script1 and script2.

8 comentarios

Suhail Najm Abdullah
Suhail Najm Abdullah el 20 de Dic. de 2018
True. Thanks alot.
Thi Huong Hoa Trinh
Thi Huong Hoa Trinh el 12 de Jun. de 2020
Thanks so much
Mathlabuser 29952
Mathlabuser 29952 el 11 de Jul. de 2024
So the functions must be part of the "main" file (say at the end), unless they are objectified like the second comment below suggests?
Functions can be local to a script, or they can be independent files themselves.
% this is a script
mynumbers = [1 2 3 4];
% call the external function file (attached)
A = numtimes2(mynumbers)
A = 1x4
2 4 6 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% call the local function (below)
B = numtimes3(mynumbers)
B = 1x4
3 6 9 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% this is a local function in a script
function output = numtimes3(input)
output = input*3;
end
See also:
Mathlabuser 29952
Mathlabuser 29952 el 12 de Jul. de 2024
Thanks! I'll try. The script can be saved as script.m as well and call the numtimes2.m?
DGM
DGM el 12 de Jul. de 2024
Yes. A script can call its own local functions, or it can call a function file like numtimes2().
Koen
Koen el 10 de Abr. de 2025
Editada: Koen el 10 de Abr. de 2025
Can you also put all these (I have quite a few functions) new functions into some folder; for example workspace/functions/myFunction.m?
And how would you call that?
EDIT: I found that you can call
addpath("functions");
myFunction();
In this case.
DGM
DGM el 10 de Abr. de 2025
You got it.
You can also edit the path using pathtool if you find it more convenient.

Iniciar sesión para comentar.

Más respuestas (1)

Mahmoud Khaled
Mahmoud Khaled el 28 de Mzo. de 2018
Editada: Mahmoud Khaled el 27 de Jul. de 2020
You can add them to a MATLAB class. Then instantiate an object of this class and call any of the functions.
It should be something like this:
In a separate file (ex, functionsContainer.m)
classdef functionsContainer
methods
function res = func1(obj,a)
res = a * 5;
end
function res = func2(obj,x)
res = x .^ 2;
end
end
end
Then, in your script create an object:
myObj = functionsContainer;
Finally, call whatever function you like:
res1 = myObj.func1(a);
res2 = myObj.func2(x);

6 comentarios

riki ragùa
riki ragùa el 25 de Abr. de 2018
can you explaine more or give us example please ?
Mahmoud Khaled
Mahmoud Khaled el 27 de Jul. de 2020
@riki: i upadated my answer.
If you wanted to do this I'd make those functions Static, since they don't need or use any state from the object itself.
classdef functionsContainer
methods (Static)
function res = func1(a)
res = a * 5;
end
function res = func2(x)
res = x .^ 2;
end
end
end
Use:
y = functionsContainer.func1(2) % 10
Another way to make local functions available outside their file is to have the main function return function handles to those local functions.
function [fh1, fh2] = example328959
fh1 = @func1;
fh2 = @func2;
end
function y = func1(a)
y = 5*a;
end
function z = func2(b)
z = b.^2;
end
Use as:
[f1, f2] = example328959;
f1(2) % also 10
Scott Shelton
Scott Shelton el 15 de Nov. de 2022
Editada: Scott Shelton el 15 de Nov. de 2022
Is there a way for example328959 to be inputed from a string?
I have a variable that stores example328959 as "example328959" as I need to be able to change the file that is referenced. Is there someway to reference this string as the file name in my "Use as:" code?
if you define the methods as static, you dont even have to instantiate the class
E.g:
classdef Functions
methods(Static)
function y = func1(x)
% body
end
function y = func2(x)
% body
end
end
end
And then you can run from another script or cmd:
output = Functions.func1(input)

Iniciar sesión para comentar.

Categorías

Más información sobre Functions en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Mzo. de 2017

Comentada:

DGM
el 10 de Abr. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by