Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'call' Matlab functions from another script

Tags:

matlab

My Matlab script .m file is getting too big. I want to move functionality to multiple .m files my moving functions from the primary file to a several other .m files, each based on category of functionality.

How can the primary .m file 'call' functions in these other new .m files?

like image 219
Doug Null Avatar asked Jul 27 '15 15:07

Doug Null


People also ask

Can you call a function in a script MATLAB?

You call the function just like you would call it from the command line or from another function. There is no difference in the syntax. Are you trying to have the function code in the same file as the script code?

How do I call a MATLAB file from another MATLAB file?

if name. m exist in the same directory and if you want to call this file in name2. m, then just type 'name;' inside name2. m, no extension is needed!

How do you call a function in a MATLAB live script?

You can run live functions using several methods, including calling them from the Command Window or calling them from a live script. In MATLAB Online™, you also can use the Run button. To run a live function from the Command Window, enter the name of the function in the Command Window. For example, use mymean.

How do I call my function in MATLAB?

To call a function, we need to write in a line of code in the following order, output argument, followed by the “=” character, function name, and then the input arguments in parentheses.


1 Answers

All the information provided in my answer can be found in Function Basics at MATHWORKS.

How to make a function in MATLAB? You use the following template. In addition the name of the function and the name of the file should be similar.

% ------------------- newFunc.m--------------------
function [out1,out2] = newFunc(in1,in2)
out1 = in1 + in2;
out2 = in1 - in2;
end
%--------------------------------------------------

For using multiple functions you can apply them either in separate m-files or using nested/local structures:

Separate m-files:

In this structure you put each function in a separate file and then you call them in the main file by their names:

%--------- main.m ----------------
% considering that you have written two functions calling `func1.m` and `func2.m`
[y1] = func1(x1,x2);
[y2] = func2(x1,y1);
% -------------------------------

local functions:

In this structure you have a single m-file and inside this file you can define multiple functions, such as:

% ------ main.m----------------
    function [y]=main(x)
    disp('call the local function');
    y=local1(x)
    end

    function [y1]=local1(x1)
    y1=x1*2;
    end
%---------------------------------------

Nested functions:

In this structure functions can be contained in another function, such as:

%------------------ parent.m -------------------
function parent
disp('This is the parent function')
nestedfx

   function nestedfx
      disp('This is the nested function')
   end

end
% -------------------------------------------

You cannot call nested function from outside of the m-files. To do so you have to use either separate m-files for each function, or using class structure.

like image 171
NKN Avatar answered Oct 24 '22 19:10

NKN