How MATLAB Reads an M File?

1 visualización (últimos 30 días)
Rightia Rollmann
Rightia Rollmann el 4 de Mzo. de 2017
Respondida: John D'Errico el 4 de Mzo. de 2017
I have the two following functions in a MATLAB M File. Am I right that when you run an M File in Matlab, MATLAB ALWAYS executes the first function (here myfun), and does't run others unless I execute them (e.g., typeing and entering myfun2)?
How Can I tell MATLAB to read the second function in the M File first?
function myfun
b = 1
end
function myfun2
if exist('b')
disp('exists');
elseif ~exist('b')
disp('does not');
end
end

Respuestas (1)

John D'Errico
John D'Errico el 4 de Mzo. de 2017
NO. MATLAB does not "read" any function, as you seem to be thinking.
The secondary functions (beyond that main one) are accessible only when called by the main function. While (if you know what you are doing) you can extract function handles to allow you to use those secondary functions, why bother?
Just save the other functions in their own distinct m-files. Now you can use them in any order you wish.

Categorías

Más información sobre Adding custom doc en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by