18

I'm quite new to Matlab. I've defined a function inside a .m file, I want to use that function in that .m file inside another .m file, and I want to run the contents of that last .m file from the command window.

How should I go about accomplishing this?

EDIT- for clarification, I have one function a inside a.m, and a script inside b.m that uses the function a inside a.m. I would like to run this script inside b.m from the command window, but am not sure how to do so. (as a side note, I can totally convert the script in b.m into a function if need be)

EDIT- right now I just need to know how to import/load a matlab file and that is it!!!

wrongusername
  • 18,564
  • 40
  • 130
  • 214
  • Closely related: [Is it possible to define more than one function per file in MATLAB?](http://stackoverflow.com/q/3569933/52738) – gnovice Jun 11 '11 at 18:30
  • @gnovice - thank you, but unfortunately I have no desire yet to define more than one function in a file. Sorry that the question wasn't clear enough. – wrongusername Jun 11 '11 at 20:32

3 Answers3

16

If I understand your situation correctly, you have something like this:

A file (`A.m'):

function results = A(parameters)
   % some code

A file (`B.m'):

function results = B(parameters)
   % some code

You want to use function A inside B, you can just call that function from inside function B:

function results = B(parameters)
   % some code
   otherResults = A(otherParameters)

If your situation is something like what nimrodm described, your A.m file is something like:

 function results = A(paramters)
    % some code
    function results = C(parameters)
        % code of function C
    end
 end
    function results = D(parameters)
        % code of function D
    end

There is no way of directly accessing C and D from outside A. If you need to use subfunction D outside of A, just make a file D.m containing

function results = D(parameters)
    % code of function D
end

And preferably, removed the same code from function A.

For a nested function C, the same can be done in some (but not all) cases, as nested functions also have access to the variables of function A. In recent versions of MATLAB (I guess R2010b or R2011a), the editor highlights variables that are shared between a function and nested functions in teal. If you don't make use of the variables of function A inside of function C, just do the same as for function D. If you do, pass these variables as parameters and/or return values and adjust the rest of your code to reflect this. Test your code and afterwards, do the same as for D.

Most likely, you will not have case C, as this is an advanced feature in MATLAB.

There is however another case, if you are not using MATLAB functions, but MATLAB scripts in different files. You can call a script (both from command line and another function or script, just by its (file) name.

contents of file E.m:

% code for script E

contents of file F.m:

% some code
E;

Using that code, you execute all commands in E from inside script F. Beware that E and F will share all their variables, so if you begin your scripts by something like clear all; close all; clc;, you cannot pass any variables from F into E (and you will lose all results from F calculated before calling E.

In most cases it is better to use functions instead of scripts, so that's also the way to solve such a situation: make everything into functions with decent parameters and return values.

edit: After you 'changed' your question, it's quite easy.

Let's consider you have the function, I will use different names, as that is more intuitive to understand. You have the function ackermann inside the file ackermann.m which you want to call from the script bigScript.m.

The file ackermann.m contains the Ackermann-Péter function (as an example):

function result = ackermann(m,n)
  if m == 0
      result = n + 1;
  elseif m > 0
      if n == 0
          result = ackermann(m-1,1);
      elseif n > 0
          result = ackermann(m-1,ackermann(m,n-1));
      else
          error('n has to be positive');
      end
  else
      error('m has to be positive');
  end
end

From inside your big script, you can call the function ackermann as follows (if you want m = 1 and n = 1):

A = ackermann(1,1)

It's that simple, no need to load anything. But you need to remember to have the function 'available in your path', the easiest way to do this, is just keep the script and function files in the same directory.

Anyhow, I sense you are a starting MATLAB user: if you don't know what a function does, just type help functionname (substituting functionname of course) into the command window. You will notice that the function load is there to load data files, not for m-files (as the m-files in your path are used automatically).

Egon
  • 4,757
  • 1
  • 23
  • 38
  • 2
    Hm, thanks Egon, but I'm assuming that I would need to somehow import another `.m` file to use it, just like in C++ and python and other languages. How would I do this, since `load a.m` gives me an error about number of columns being different? Also, I will update the question to make it clearer. – wrongusername Jun 11 '11 at 20:35
  • 2
    @wrongusername It turns out that you don't need to do anything to "load" function1.m into Matlab. If function1.m and function2.m are in the same folder, then you can call function1() from inside function2.m() – solvingPuzzles Apr 24 '12 at 02:41
14

In principle, MATLAB advocates the use of one function per .m file. You can call such a function from another .m file and from the MATLAB command line.

You can define multiple functions in one .m file, but only the first (or 'outermost') function can be accessed from other .m files or the command line. The other functions are treated as 'helper' functions that may be called only inside this particular .m file.

nimrodm
  • 23,081
  • 7
  • 58
  • 59
  • Hi nimrodm, how would I "call such a function from another .m file"? That's is all I need to know to answer this question, thanks! – wrongusername Jun 12 '11 at 20:57
  • If you have an .m file with the name 'myfunc' ( and the first non comment line in that file reads like "function y=myfunc(x)" ) you just write y1=myfunc(x1). – nimrodm Jun 13 '11 at 08:19
  • thanks a lot! But how would I import or load such a file? As I would expect, I get `??? Undefined function or variable 'getBz'.` without doing anything beforehand. – wrongusername Jun 13 '11 at 17:50
  • 2
    There is no 'import' statement in matlab. You just have to make sure the myfunc.m file is in the MATLAB path. Try addpath('') or just add it using the GUI (File menu I believe). – nimrodm Jun 13 '11 at 19:13
13

For anyone else searching for this question, as I did, just type:

addpath('[Path name of mat file]');

This will tell Matlab how to find the function. To verify, just type:

which [function name]

If successful, it should list the path name that you just added.

styrofoam fly
  • 578
  • 2
  • 9
  • 25
Phoeniyx
  • 542
  • 4
  • 15