Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  1. Is it possible to have the function defined in my_file.m?
  2. If not, suppose I have it defined in f.m. How do I call it in my_file.m?

I read the online documentation, but it wasn't clear what is the best way to do this.

like image 412
Viktor Avatar asked Mar 19 '11 16:03

Viktor


People also ask

Can a script and a function be defined in the same file?

As of MATLAB 2016b, functions can be defined in scripts.

Can you put a function inside a script?

Short answer: Yes. As long as you load the first script containing the function first, you can call the function anywhere, as long as it's loaded first. In this example, make sure file1. js contains your largeFunction() function.

What is the difference between scripts and functions in MATLAB?

Scripts versus Functions Scripts are m-files containing MATLAB statements. MATLAB ``functions'' are another type of m-file. The biggest difference between scripts and functions is that functions have input and output parameters. Script files can only operate on the variables that are hard-coded into their m-file.

What is the difference between function files and script files in MATLAB?

Scripts are the simplest type of code file, since they store commands exactly as you would type them at the command line. However, functions are more flexible and more easily extensible. To calculate the area of another triangle using the same script, you could update the values of b and h in the script and rerun it.


2 Answers

As of release R2016b, you can have local functions in scripts, like so:

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

Prior to release 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 to simple operations, since they have to be defined in a single expression. For more complicated functions, you will have to define them in their own files, place them somewhere on the MATLAB path to make them accessible to your script, and then call them from your script as you would any other function.

like image 129
gnovice Avatar answered Sep 22 '22 06:09

gnovice


The way I get around this limitation, is to turn my scripts into functions that take no arguments (if I need variables from the global namespace, I either explicitly pass them in the function, or use "evalin" to grab them.)

Then you can define all the additional functions you need in the "script." It's a hack, but I have found it to be quite powerful in those cases where I need several non-trivial functions.

EDIT: Here's a simplistic example. All this can reside in a single 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 
like image 44
John Avatar answered Sep 21 '22 06:09

John