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'):
A file (`B.m'):
You want to use function
A
insideB
, you can just call that function from inside function B:If your situation is something like what nimrodm described, your
A.m
file is something like:There is no way of directly accessing
C
andD
from outsideA
. If you need to use subfunctionD
outside ofA
, just make a fileD.m
containingAnd 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 functionA
. 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 functionA
inside of functionC
, just do the same as for functionD
. 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 forD
.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
:contents of file
F.m
:Using that code, you execute all commands in
E
from inside scriptF
. Beware thatE
andF
will share all their variables, so if you begin your scripts by something likeclear all; close all; clc;
, you cannot pass any variables fromF
intoE
(and you will lose all results fromF
calculated before callingE
.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 fileackermann.m
which you want to call from the scriptbigScript.m
.The file
ackermann.m
contains the Ackermann-Péter function (as an example):From inside your big script, you can call the function
ackermann
as follows (if you want m = 1 and n = 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 functionload
is there to load data files, not for m-files (as the m-files in your path are used automatically).