Can I call a function defined within another function .m file?
Mostrar comentarios más antiguos
Hello. Consider a file titled giveA.mat :
function A = giveA(argA)
% Main function
b = giveB(argB);
...
end
function b = giveB(argB) % defined within one file
% simple sub-function
end
% End of file
I'd like to call giveB() somewhere else in the code, but it would be very convenient for me to keep this function as is.
Is there a syntax which will allow this, i.e. can I call giveB() from, say someOtherFunction(). I expect something like
b = givA(argA)/giveB(argB)
or
b = giveA(argA)>giveB(argB)
as this is how Matlab displays location of giveB() in error messages.
Respuesta aceptada
Más respuestas (2)
Fangjun Jiang
el 21 de Jun. de 2018
0 votos
No. It is a local function. If it needs to be used outside, it has to be saved in a separate .m file.
OCDER
el 21 de Jun. de 2018
The next closest thing is to define an object class with static methods (give.m)
%give.m
classdef give
methods(Static)
function A = A(argA)
A = argA;
disp('I gave A');
end
function B = B(argB)
B = argB;
disp('I gave B');
end
end
end
To use "giveA", use "give.A" instead.
D = give.A(3)/give.B(10);
Categorías
Más información sobre Function Creation en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!