|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help > programs > matlab |
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.
which plot" in a fairly recent matlab it will tell you that
the plot function is built-in to matlab*.m files. If you type
"which fliplr" you'll find out where the fliplr.m
file is.*.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.which histc" will show you
where the histc 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.
foo - what will happen?
foofoo. A matlab *.m
file can contain several subfunctions. These can only be called from within
the file.foo matlab will look for
a private function. If in the directory containing the original matlab file
there's a subdirectory called private then the functions in
this private directory can only be called by files in the
parent directory.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 typing path. You can modify this list
of directories if you want but the default should be ok.
which commandwhich command can be used in more sophisticated ways than
as used above
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 the which can help solve mysterious bugs.which restore" draws a blank, but "which restore in print" shows that
when on my system the print function is run it has access to
a function called "restore" that isn't normally visible.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.
@. 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.
| | computing help | Matlab | |