Subfunctions
MATLAB program files can contain code for more than one function. Additional functions within the file are called subfunctions, and these are only visible to the primary function or to other subfunctions in the same file.
Each subfunction begins with its own function definition line. The functions immediately follow each other. The various subfunctions can occur in any order, as long as the primary function appears first
When you call a function from within a program file, MATLAB first checks the file to see if the function is a subfunction. It then checks for a private function (described in the following section) with that name, and then for a standard program file or built-in function on your search path. Because it checks for a subfunction first, you can override existing files using subfunctions with the same name.
Private functions
Private functions are functions that reside in subfolders with the special name private. These functions are called private because they are visible only to functions and scripts that meet these conditions:
A function that calls a private function must be defined in a program file that resides in the folder immediately above that private subfolder.
A script that calls a private function must itself be called from a function that has access to the private function according to the above rule.
For example, assume the folder newmath is on the MATLAB search path. A subfolder of newmath called private can contain functions that only the functions in newmath can call.
Because private functions are invisible outside the parent folder, they can use the same names as functions in other folders. This is useful if you want to create your own version of a particular function while retaining the original in another folder. Because MATLAB looks for private functions before standard functions, it finds a private function named test.m before a nonprivate program file named test.m.
Primary functions and subfunctions can also be implemented as private functions.
Subscripting and Indexing
Subscripting
Indexing
|
Example |
Result |
|
m = [1 2] n = LEDcolor(m) p = n(LEDcolor.GREEN) |
m = 1 2 n = GREEN RED p = GREEN |
String evaluation
String evaluation adds power and flexibility to the MATLAB language, letting you perform operations like executing user-supplied strings and constructing executable strings through concatenation of strings stored in variables.
Eval
The eval function evaluates a string that contains a MATLAB expression, statement, or function call. In its simplest form, the eval syntax is: eval('string').
Feval
[y1, y2, ...] = feval(fhandle, x1, ..., xn)evaluates the function handle, fhandle, using arguments x1 through xn. If the function handle is bound to more than one built-in or .m function, (that is, it represents a set of overloaded functions), then the data type of the arguments x1 through xn determines which function is dispatched to.
Command/Function duality
In MATLAB, these statements are equivalent:
load durer.mat % Command syntax
load('durer.mat') % Function syntax
This equivalence is sometimes referred to as command-function duality.
All functions support this standard function syntax:
[output1, ..., outputM] = functionName(input1, ..., inputN)
If you do not require any outputs from the function, and all of the inputs are literal strings (that is, text enclosed in single quotation marks), you can use this simpler command syntax:
functionName input1 ... inputN
With command syntax, you separate inputs with spaces rather than commas, and do not enclose input arguments in parentheses. Because all inputs are literal strings, single quotation marks are optional, unless the input string contains spaces. For example:
disp 'hello world'
When a function input is a variable, you must use function syntax to pass the value to the function. Command syntax always passes inputs as literal text and cannot pass variable values. For example, create a variable and call the disp function with function syntax to pass the value of the variable:
A = 123;
disp(A)
This code returns the expected result: 123
You cannot use command syntax to pass the value of A, because this calldisp A
is equivalent to: disp('A'),and returnsA
