Programming and Data Types    

Calling Functions

When you call M-file or built-in functions, you often have the choice of using one of two formats for the function call. (The same applies to entering commands at the MATLAB command line.)

You can code your function calls in either a function or command syntax. This is referred to in MATLAB as command/function duality.

Function Syntax

Function calls written in the function syntax look essentially the same as those in many other programming languages. One difference is that, in MATLAB, functions can return more than one output value.

A function call with a single return value looks like this:

If the function returns more than one value, separate the output variables with commas or spaces, and enclose them all in square brackets ([]).

Here are two examples:

In the function syntax, MATLAB passes arguments to the function by value. See the examples below, under Passing Arguments.

Command Syntax

A function call made in command syntax consists of the function name followed by one or more arguments separated by spaces.

While the command format is simpler to write, it has the restriction that you may not assign any return values the function might generate. Attempting to do so generates an error.

Two examples of command syntax are

In the command syntax, MATLAB treats all arguments as string literals. See the examples in the following section.

Passing Arguments

Function calls written in function syntax pass their arguments by value. That is, for those arguments expressed as variables, it is the value of the variable that gets passed to the function.

Function calls written in command syntax pass all arguments as string literals. If you pass a numeric value, such as 75 in the example below, MATLAB passes it as the string, '75'.

The following examples show the difference between passing arguments in the two syntaxes.

Example 1.   Calling disp with the function syntax, disp(A), passes the value of variable A to the disp function.

Calling it with the command syntax, disp A, passes a string with the variable name, 'A'.

Example 2.   Passing two variables representing equal strings to the strcmp function using function and command syntaxes gives different results. The function syntax passes the values of the arguments. strcmp returns a 1, which means they are equal.

The command syntax passes the names of the variables, 'str1' and 'str2', which are unequal.

Passing String Arguments

When using the function syntax to pass a string literal to a function, you must enclose the string in single quotes, ('string').

For example, to create a new directory called myapptests, use

On the other hand, variables that contain strings do not need to be enclosed in quotes.

Passing Arguments in a Structure

Instead of requiring an additional argument for every value you want to pass in a function call, you can package them in a MATLAB structure and pass the structure. Make each input you want to pass a separate field in the structure argument, using descriptive names for the fields.

Structures allow you to change the number, contents, or order of the arguments without having to modify the function. They can also be useful when you have a number of functions that need similar information.

Passing Arguments in a Cell Array

You also can group arguments into cell arrays. The advantage over structures is that cell arrays are referenced by index, allowing you to loop through a cell array and access each argument passed in or out of the function. The disadvantage is that you don't have fieldnames to describe each variable.


  Current Date and Time Obtaining User Input