matlab_-_language
Differences
This shows you the differences between two versions of the page.
matlab_-_language [2012/11/12 18:30] – created jochen | matlab_-_language [2012/11/12 22:02] (current) – added function definitions jochen | ||
---|---|---|---|
Line 15: | Line 15: | ||
* **comma** (,): also separates statements (if not used within a statement, that is), but does not suppress the output | * **comma** (,): also separates statements (if not used within a statement, that is), but does not suppress the output | ||
* **ellipse** (...): three consecutive dots work as a " | * **ellipse** (...): three consecutive dots work as a " | ||
+ | |||
+ | In addition, whenever Matlab encounters a word (sequence of alphanumeric characters beginning with a letter) that is not part of a string, a keyword, or addressing a fieldname of a structure (such as in '' | ||
+ | |||
+ | * variables (names referenced in the current workspace) | ||
+ | * nested functions | ||
+ | * locally defined function (in the same scope/ | ||
+ | * private functions (functions defined in any folder called " | ||
+ | * if an argument is an object, a class method (of the corresponding class, in order of arguments, which is the way to overload built-in functions) | ||
+ | * built-in functions | ||
+ | * class-constructor functions ('' | ||
+ | * function names (in order of path precedence, within folder precedence is MEX, P-file, M-file) | ||
+ | |||
+ | This means that functions (built-in as well as user defined) can always be overridden by variables and appropriate class methods (e.g. for the '' | ||
===== Comments ===== | ===== Comments ===== | ||
Line 26: | Line 39: | ||
===== Control flow elements ===== | ===== Control flow elements ===== | ||
- | When writing code, it is often crucial to either | + | When writing code, it is often crucial to be able to |
+ | * define new functions that provide access to repeatedly used code | ||
* execute certain expressions only conditionally (if and only if a condition is met or not met) | * execute certain expressions only conditionally (if and only if a condition is met or not met) | ||
* execute an expression or list of expressions repeatedly (either for a predetermined number of iterations or until an exit criterion is met) | * execute an expression or list of expressions repeatedly (either for a predetermined number of iterations or until an exit criterion is met) | ||
Line 32: | Line 46: | ||
* be able to react to an error that occurred as part of a statement without leaving the code | * be able to react to an error that occurred as part of a statement without leaving the code | ||
* return from a function prematurely (e.g. in case nothing else can be done that makes sense) | * return from a function prematurely (e.g. in case nothing else can be done that makes sense) | ||
+ | |||
+ | ==== Defining new functions ==== | ||
+ | As explained above, Matlab interprets any word it encounters as potentially belonging to a function. To define a new function, all that is needed is to create a new file and add the desired function declaration on top (first line of actual code after optional comments): | ||
+ | |||
+ | <code matlab> | ||
+ | |||
+ | % further optional comments | ||
+ | list_of_outputs = some_code(list_of_inputs);</ | ||
+ | |||
+ | The list_of_outputs and list_of_inputs words (identifiers) are optional, depending on the desired functionality. Sometimes, a function is not required to return any useful value (e.g. a function instructing Matlab to wait until a desired point in time is reached), or it requires no input (e.g. a function used to determine the currently active figure). However, in most situations (particularly where computations are performed), user defined functions will have both inputs (variables passed into the function such that the function has access to the values stored therein) and outputs (variables that, once the function has completed executing, will be passed back to the code that called the function). | ||
+ | |||
+ | As an additional language construct (which can be considered as pseudo-reserved keywords), the two identifiers '' | ||
+ | |||
+ | <code matlab> | ||
+ | |||
+ | % storing the sum of the first two arguments in the third output | ||
+ | varargout{3} = varargin{1} + varargin{2};</ | ||
+ | |||
+ | To make such code reliable, it is usually advisable to test the number of given inputs as well as the number of requested outputs with the corresponding '' | ||
+ | |||
+ | One last important fact about functions: While Matlab does not create a copy of the variables before they are passed into a function, Matlab will not alter the content of a variable as it exists within the workspace that calls the function. In other words, if the following function is called: | ||
+ | |||
+ | <code matlab> | ||
+ | |||
+ | % add 1 to input | ||
+ | x = x + 1;</ | ||
+ | |||
+ | in the following way: | ||
+ | |||
+ | <code matlab>% define a variable | ||
+ | somevar = 10; | ||
+ | |||
+ | % call add1tox | ||
+ | add1tox(somevar);</ | ||
+ | |||
+ | The variable somevar will still have the value of 10! This is true regardless of the type or size of the variable (unless the storage is only referenced by the variable, such as for graphics handles or user defined objects)! | ||
==== Conditional code execution ==== | ==== Conditional code execution ==== |
matlab_-_language.txt · Last modified: 2012/11/12 22:02 by jochen