In MATLAB, can I have a script and function definition in the same file?

Suppose I have a f() function and I want to use it in my_file.m , which is a script.

  • Is it possible for a function to be defined in my_file.m ?
  • If not, suppose it is defined in fm . What should I call it in my_file.m ?

I read the online documentation, but it was not clear how best to do this.

+63
function file matlab
Mar 19 '11 at 16:48
source share
7 answers

Starting with version R2016b, you can have local functions in scripts , for example:

 data = 1:10; %# A vector of data squaredData = f(data); %# Invoke the local function function y = f(x) y = x.^2; end 

Prior to the release of R2016b, the only type of function that could be defined inside a MATLAB script was an anonymous function . For example:

 data = 1:10; %# A vector of data f = @(x) x.^2; %# An anonymous function squaredData = f(data); %# Invoke the anonymous function 

Note that anonymous functions are better suited for simple operations, as they must be defined in a single expression. For more complex functions, you will have to define them in your files, place them somewhere in the MATLAB path to make them available to your script, and then call them from the script, like any other function.

+39
Mar 22 2018-11-11T00:
source share

How do I get around this limitation, I need to turn my scripts into functions that take no arguments (if I need variables from the global namespace, I either explicitly pass them into functions, or use "evalin" to capture them.)

Then you can define all the additional functions that you need in the "script". This is a hack, but I found it powerful enough in cases where I need several non-trivial functions.

EDIT: Here's a simplified example. All this can be in one file.

 function [] = myScriptAsAFunction() img = randn(200); img = smooth(img); figure(1); imagesc(img); axis image; colorbar; end function simg = smooth(img) simg = img / 5; end 
+32
06 Feb 2018-12-12T00:
source share

You can do something like this (if your file is named my_file.m ):

 function my_file %script here end function out = f(in) %function here end 

If you press the start button, the my_file function will be executed by default.

+16
Nov 06 '13 at 13:23
source share

1) You cannot insert a function inside a script.

2) Make sure that fm is in your path or in the current directory, and you can call it like any other function.

+5
Mar 19 '11 at 17:05
source share

Starting with R2016b , you can define local functions in a script.

 x = 1; y = add1(x); function z = add1(x) z = x + 1; end 
+4
Jul 11 '16 at 13:37
source share

I implemented a solution from John , and I found it useful. But there are a few caveats (in Octave, Matlab may behave similarly):

  • If the code inside your main function contains clear all before using the helper function, this will not work. In test3.m commenting / uncommenting clear all causes the code to work / not work.

     function [] = test3() %clear all a = myfunc( 1 ); a endfunction; %--------------------------------- % Auxiliary functions function retval = myfunc( a ) retval = 2 * a; endfunction; 

    From It seems that when the script starts, the first pass is executed, where the external functions of the code are executed (in this case, such code is missing), and the functions are defined (in this case test3 and myfunc ) are added to the workspace. The second pass will perform the main function that myfunc will not find if clear all active.

  • As indicated by chessofnerd, because of the box, the variables in your main function do not fall into the workspace.

+2
Mar 26 '16 at 12:56
source share

A sample file can have many functions. But only the first can act as the main function when you run the file. Others can only be used in this file. For some situation, you want to define a large function. You can divide it into smaller functions and define it below.

However, the easiest way to find the answer is to try ~

+1
Sep 22 '15 at 1:34
source share



All Articles