Loren on the Art of MATLAB

Turn ideas into MATLAB

Duality Between Function and Command Syntax 4

Posted by Loren Shure,

A long time ago, I covered the topic of the duality between command and function syntax.

Contents

What is Command-Function Duality?

For calling functions with only literal string input values, i.e., strings delimited by the usual single quote ('), you can avoid the overhead of using parentheses and comma separators to invoke the function, especially if you want either no output or only one output. Let me show you an example.

Checking Directory Contents

Suppose I want to see the MATLAB code files in my directory beginning with the letter 'q'. Here are three ways to do such a query.

First way:

dir q*.m
qichen31.m  quadvec.m   quantum.m   

Second Way:

dir('q*.m')
qichen31.m  quadvec.m   quantum.m   

Third Way:

out = dir('q*.m')
out = 
3x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

Discussion of Results

From these three methods, you can see that the first two give the same output, a list of the names of matching files. When using the command syntax (the first way), you can't store the output in a named variable. In this case, the decision was to print something out in a useful format, but not to place the information into a variable for further programmatic use.

The second result is equivalent to the first. You can see the duality looking at these two statements. The first and second are equivalent. Simply replace the space and following string (or strings) into a comma-separated list of the same string(s) inside parentheses. This means that the statement myfun A B c is equivalent myfun('A','B','c').

And now for the third statement. Clearly it is different than the first two. First, you can see that something is returned in the output variable out. Second, you can see that out is not simply a list of names, but a struct containing several fields with information, including name, date, etc.

Default MATLAB Behavior

MATLAB functions often return at least one output, even if the user does not supply an output variable. If the function does return an output when the user does not specify one, the result goes into a variable named ans.

Overriding Default Behavior

It is possible to override default behavior for your function outputs. To do so, we take advantage of the function nargout. This function allows us to query how many output variables the function is called with. In the case of the function dir, you can imagine the logic of the code goes something like this:

  1. check nargout
  2. if nargout is 0, get and print the list of matching files/directories
  3. if nargout is greater than 0, collect the relevant directory information and place it into a structure

With the command form for calling a MATLAB function, the value for nargout is 0. Despite this, some functions are designed to return a value, though without a specified output, ans is created or updated.

What About You?

Do you take advantage of command-function duality? In functions you create? At the MATLAB prompt or in application code your write? Let me know here.


Get the MATLAB code

Published with MATLAB® R2013a

17 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.