Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Matlab to import another .m file

Tags:

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

like image 512
wrongusername Avatar asked Jun 11 '11 08:06

wrongusername


People also ask

How do I run a .m file from another file in MATLAB?

Direct link to this comment 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 I import an M file into MATLAB?

m in the current MATLAB folder or directory, you can execute the commands in the m-file by simply typing filename at the MATLAB command window prompt. If you don't want to run the whole m-file, you can just copy the part of the m-file that you want to run and paste it at the MATLAB prompt.

How do I import data from one MATLAB file to another?

Open the Import Tool MATLAB® Toolstrip: On the Home tab, in the Variable section, click Import Data. MATLAB command prompt: Enter uiimport( filename ) , where filename is a character vector specifying the name of a text or spreadsheet file.


1 Answers

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

like image 52
Egon Avatar answered Sep 29 '22 07:09

Egon