Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

January 11th, 2006

Mental Model for feval

What's your mental model for when to use the MATLAB function feval?

Until Release 14 (MATLAB 7), feval was the way to evaluate a function handle. By that I mean you might write code something like this:

function y = halfcircle(fun,n)
if nargin < 2
    n = 20;
end
y = feval(fun,0:pi/n:pi);

And you would call this function like this:

ys = halfcircle(@sin);
yc = halfcircle(@cos);

feval can also be used to evaluate functions when they are specified by their name.

fn = 'sin';
y = feval(fn, 0:pi/3:pi);

It is more direct however to call the function itself instead of passing it to feval:

y = sin(0:pi/3:3);

In Release 14, we removed the need to use feval to evaluate function handles. Instead, they can be evaluated directly. Here's what the halfcircle code might look like in MATLAB 7.

function y = halfcircle(fh,n)
if nargin < 2
    n = 20;
end
y = fh(0:pi/n:pi);

So, I was curious recently when I saw some code posted in the MATLAB newsgroup containing a line that looked like this:

files = feval('dir','*.gif');

This was interspersed with code containing calls to length, imread, and several other functions, all of which were called directly.

Does anyone have insight about what distinction a user might have made between the MATLAB function dir and other ones?

Please post your comments and thoughts. I'd love to hear how you think about this.

9 Responses to “Mental Model for feval”

  1. per isakson replied on :

    Functions, which take strings as input arguments, may be used in command form. Some of these are know as commands in other environments, e.g. DIR in DOS. DIR in Matlab thus becomes not as a Matlab function, but rather a shortcut to DIR of the underlying OS. It is possible to overlook that in Matlab the function form is valid for DIR. The command form doesn’t return an argument. Thus, you can see contructs like “files = feval(’dir’,'*.gif’);” and “files = evalc( ‘dir *.gif’ );”

    / per

  2. Yang replied on :

    Yeah, I think the benefit of usage of the “feval” function is to return a structure argument like the ways described in “More on eval”.

  3. loren replied on :

    Is the doc for dir not clear perhaps? You can get this structure of output by using dir directly also:

    fstr = dir(’*.gif’)
    in the help description it says:

    files = dir('directory') returns the list of files in the specified directory (or the current directory, if dirname is not specified) to an m-by-1 structure with the fields: name, date, bytes, isdir.

  4. per isakson replied on :

    RTFM. The problem (to some of us) is that we already have mental models of *commands* like DIR, SAVE, and PRINT when we try Matlab for the first time. Our models work well with Matlab when we start using it *interactively*. Our models are so well established that *looking through* the documentation of the commands does not change our models.

    To me a problem with the Matlab documentation is that I tend to overlook powerful (added) features of functions because they are “hidden” in the body text of the descriptions and not addressed by the examples.

  5. krishna kr tiwari replied on :

    I am a post graguate student.Studing in master of eectronics science.I currently worked on MATLAB but nobody know about the MATLAB.So please you give me a proper guiedens.

    krisna kr tiwari
    IPS ACADEMY.Indore(M.P.)
    INDIA

  6. loren replied on :

    Krisna-

    Welcome to the MATLAB community! I recommend you read Getting Started with MATLAB, run demos, read the Usenet MATLAB newsgroup comp.soft-sys.matlab, and watch this blog for helpful topics.

  7. Ben replied on :

    I’ll agree with Per here: I certainly fall victim to “transparency to documentation” occasionally. But sometimes that’s more of a problem with the user (including myself) that MATLAB shouldn’t have to fix; certainly big things like “Notes For C Programmers” are completely necessary and put up appropriately large red flags for common mental model errors when jumping between C and MATLAB.

    However an advantage with MATLAB is that you can browse the source of many MATLAB functions to get a direct view (and a good complement to documentation) of what’s going on under the hood, unless they are builtin, like ‘dir’ :-(.

  8. Matthew Simoneau replied on :

    I’m constantly surprised to find relatively sophisticated MATLAB users who are unfamiliar with this “command/function duality”. A section in the MATLAB documentation describes it clearly, but many people never see it:

  9. Matthew Simoneau replied on :

    FYI, the page I linked to above has moved to a new location.

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Loren: Timothee- Anonymous functions can only be a single (complicated) expression. You might be able to do what you...
  • Timothee: Is there a way to combine multiple commands in anonymous functions? ex1: fun=@(A)([V,D]=eig(A ); A*V-V*D)...
  • Loren: Here’s Cleve’s reply to Etienne: The crucial factor is the number and location of the nonzero...
  • Loren: Tristan- Nested functions can be slower in some cases currently. We know we have some opportunities to...
  • Tristan: Wow! I just tried with a global variable and it’s 5 times slower than with a argument! function...
  • Jon: Loren, I encountered this same problem and I attempted to find the answer by looking at the documentation for...
  • Tristan: “One thing that I have long wondered about is relative speed of nested functions relative to...
  • Etienne Non: Hi! I’m trying to understand why the Matlab function LU.m takes almost 20 times more time to...
  • Loren: Jonathan- The behavior you see is because the variable x has to come into inplaceTest and then a copy is made...
  • Jonathan: I am calling it from another function, but have just noticed a bit more odd behavior. Here is what...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics