User Tools

Site Tools


matlab_-_language

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

matlab_-_language [2012/11/12 19:30]
jochen created
matlab_-_language [2012/11/12 23:02] (current)
jochen added function definitions
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 "​line-continuation"​ character sequence, although this is no longer required in most instances (as Matlab detects an incomplete language construction),​ it may enhance readability of code   * **ellipse** (...): three consecutive dots work as a "​line-continuation"​ character sequence, although this is no longer required in most instances (as Matlab detects an incomplete language construction),​ it may enhance readability of code
 +
 +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 ''​structure.**FieldName**''​),​ Matlab tries to "​resolve"​ (understand) this word in the following precedence (lookup, see [[http://​www.mathworks.com/​help/​matlab/​matlab_prog/​function-precedence-order.html|function precedence order]] page on Mathworks):
 +
 +  * variables (names referenced in the current workspace)
 +  * nested functions
 +  * locally defined function (in the same scope/​function file)
 +  * private functions (functions defined in any folder called "​private"​ existing in the folder of the currently running file)
 +  * 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 (''​@class/​class.m''​)
 +  * 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 ''​@double''​ class), which offers great opportunities,​ but also potential hazards. Also, functions that are part of Matlab'​s internal toolboxes but not fully built-in (e.g. ''​var''​) can be overridden by user-defined functions. **That means that you should be careful not to re-use function names that are already defined when writing your own functions!**
  
 ===== 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>​function [list_of_outputs] = function_name(list_of_inputs)
 +
 +% further optional comments
 +list_of_outputs = some_code(list_of_inputs);</​code>​
 +
 +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 ''​**varargin**''​ and ''​**varargout**''​ can be used to pass a variable number of arguments into a function and also allow the function to return a number of variables that is determined at run-time:
 +
 +<code matlab>​function [varargout] = variable_arguments_function(varargin)
 +
 +% storing the sum of the first two arguments in the third output
 +varargout{3} = varargin{1} + varargin{2};</​code>​
 +
 +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 ''​**nargin**''​ and ''​**nargout**''​ variables which are always pre-assigned before a function is executed.
 +
 +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>​function x = add1tox(x)
 +
 +% add 1 to input
 +x = x + 1;</​code>​
 +
 +in the following way:
 +
 +<code matlab>% define a variable
 +somevar = 10;
 +
 +% call add1tox
 +add1tox(somevar);</​code>​
 +
 +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 23:02 by jochen