
- •Ministry of education and science, youth and sport of Ukraine
- •Theory MatLab programming Kinds of m-files
- •M-file structure
- •Providing help for programs
- •Creating m-Files: Accessing Text Editors
- •Scripts:
- •How functions work
- •Checking the number of function arguments
- •Passing variable numbers of arguments
- •Local and Global variables
- •Persistent Variables
- •Special Values
- •Figure 4 Flow control ‘switch’
- •Conclusion
- •References
Checking the number of function arguments
“Nargin” returns the number of input arguments passed in a call to the currently executing function. Use this nargin syntax only in the body of a function.
“nargin(function_name)” returns the number of input arguments declared for a given function, function_name. If the function has a variable number of input arguments, nargin returns a negative value.
Passing variable numbers of arguments
The varargin and varargout functions let you pass any number of inputs or return any number of outputs to a function. This section describes how to use these functions and also covers:
“Unpacking varargin Contents”
“Packing varargout Contents”
“varargin and varargout in Argument Lists”
MATLAB packs all specified input arguments into a cell array, a special kind of MATLAB array that consists of cells instead of array elements. Each cell can hold any size or kind of data – one might hold a vector of numeric data, another in the same array might hold an array of string data, and so on. For output arguments, your function code must pack them into a cell array so that
MATLAB can return the arguments to the caller.
Local and Global variables
For your MATLAB application to work with global variables:
Declare the variable as global in every function that requires access to it. To enable the workspace to access the global variable, also declare it as global from the command line.
In each function, issue the global command before the first occurrence of the variable name. The top of the M-file is recommended.
MATLAB global variable names are typically longer and more descriptive than local variable names, and sometimes consist of all uppercase characters. These are not requirements, but guidelines to increase the readability of MATLAB code and reduce the chance of accidentally redefining a global variable.
Persistent Variables
A variable may be defined as persistent so that it does not change value from one call to another. Persistent variables may be used within a function only. Persistent variables remain in memory until the M-file is cleared or changed.persistent is exactly like global , except that the variable name is not in the global workspace, and the value is reset if the M-file is changed or cleared.
Three MATLAB functions support the use of persistent variables.
Function |
Description |
mlock munlock mislocked |
Prevents an M-File from being cleared Unlocks an M-File that had previously been locked Indicated whether an M-file can be cleared or not |
Special Values
Several functions return important special values that you can use in your M-files.
Data Types
There are 14 fundamental data types (or classes) in MATLAB. Each of these data types is in the form of an array. This array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size. Two-dimensional versions of these arrays are called matrices. All of the fundamental data types are shown in lowercase text in the diagram below. An additional, user-defined data type, shown below as user class, is a subset of the structure type.
Keywords
MATLAB reserves certain words for its own use as keywords of the language.
To list the keywords, type
Figure 3 Keywords list
See the online function reference pages for help to learn how to use any of these keywords.
You should not use MATLAB keywords other than for their intended purpose.
For example, a keyword should not be used as follows:
while = 5;
??? while = 5;
Error: Expected a variable, function, or constant, found "=".
Flow Control
There are eight flow control statements in MATLAB:
• if, together with else and elseif , executes a group of statements based on some logical condition.
• switch , together with case and otherwise, executes different groups of statements depending on the value of some logical condition.
• while executes a group of statements an indefinite number of times, based on some logical condition.
• for executes a group of statements a fixed number of times.
• continue passes control to the next iteration of a for or while loop, skipping any remaining statements in the body of the loop.
• break terminates execution of a for or while loop.
• try...catch changes flow control if an error is detected during execution.
• return causes execution to return to the invoking function.