Finding Matlab functions
To understand exactly what gets run when you type a matlab function name (and where the related code is) requires quite a good understanding of matlab's basic features.
Function types
The function code itself can be in 4 main forms
- Some code is compiled into matlab itself. If you type
"
which plot
" in a fairly recent matlab it will tell you that theplot
function isbuilt-in
to matlab - Some code is contained in
*.m
files. If you type "which fliplr
" you'll find out where thefliplr.m
file is. - Sometimes the code is in a
*.p
file. These are*.m
files that have been partly processed to make them run faster the first time they're called. Unlike*.m
files these files aren't easy to read. - MEX files are functions originally written a language like C++ or F90
which have been compiled. Typing "
which histc
" will show you where thehistc
function is. Such files are operating-system dependent - Windows versions won't for example work on Macintoshes. The source code may be online but more often isn't.
Searching for Functions
Suppose in a matlab function file you have a
line that says foo
- what will happen?
- Firstly matlab will look for a variable called
foo
- It will then look for a class member function
- Then it will look for a built-in function
- If it can't find a built-in function it will look in the matlab
function file for a subfunction called
foo
. A matlab*.m
file can contain several subfunctions. These can only be called from within the file. - If there's no subfunction called
foo
matlab will look for a private function. If in the directory containing the original matlab file there's a subdirectory calledprivate
then the functions in thisprivate
directory can only be called by files in the parent directory. - If that fails, then matlab searches directory after directory looking for
something to run called
foo
. It will run the first file it finds. You can find out which directories are searched and which order they're searched in by typingpath
. You can modify this list of directories if you want but the default should be ok.
The which
command
The which
command can be used in more sophisticated ways than
as used above
- There may be several functions with the same name in
different places. Matlab will only run the first it finds but you can find out where other versions are. For example, "
which plot -all
" shows that one "plot
" function obscures another (on my system anyway). It's easy to accidentally obscure a system command by one of your own, so this use of thewhich
can help solve mysterious bugs. - Because of private functions and subfunctions, functions that are
invisible in some contexts become visible in another. Typing "
which restore
" draws a blank, but "which restore in print
" shows that when on my system theprint
function is run it has access to a function called "restore
" that isn't normally visible.
Function Handles
Sometimes (for instance when using the ODE function ode23
)
you need to pass the name of a function as an argument to another function.
As mentioned above, the context of a function call affects which function is
actually called - if you pass the function name "fun
" to
ode23
, ode23
might not call the "fun.m
" file you expect.
This is a situation where 'function handles' are useful. Rather than
pass the name 'fun'
you pass @fun
. This ensures
that the "fun
" routine that would be called from your own code
is the one that ode23
will call.
Object-oriented code
Like C++ and Java, Matlab lets you have several functions with the same name.
The particular function called is the one that matches the number
and type of arguments provided. The functions go in directories whose names
begin with @
. The Object-oriented programming with Matlab document has examples.
Again, simple use of which
isn't much help, but typing
"A=1; which foo(A)
" would tell you the file used to run the
case where the only argument to foo
is a vector of numbers.
The
"builtin
" command lets you call the original built-in function
rather than the class member function that's obscuring it.