Matlab – Using Matlab to import another .m file

matlab

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!!!

Best Solution

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).

Related Question