User Tools

Site Tools


matlab_-_language

This is an old revision of the document!


Matlab - language elements

As with any programming (or for-conversation) language, Matlab's core language has a few different types of elements that are worth understanding, as this will make it easier to read and write code that works and is robust as well as easy to understand and edit by others.

The very basic elements are:

  • comment sign (per-cent character, %): unless it appears as part of a string, everything else following this character til the end of the line is ignored by Matlab's language interpreter and meant as an explanatory help for the human reader
  • control flow keywords: these are reserved words that should not be used as part of user-defined expressions (although field names can be keywords, as they cannot be mistaken)
  • expressions: in principle, everything that Matlab “encounters” (on the command line as well as in a function or batch file) that is not a comment or a keyword is considered as an expression, which can be described as
    • a list of elements (with a minimum one element), whereas the different elements are described below, and elements using the same character set must be delimited by spaces, semicolon, or comma
    • a piece in a larger set of expressions (instruction list) whereas the order and selection of instructions is determined by the control flow keywords
    • a piece of code that makes Matlab perform internal computations (incl. user-defined function calls) and, possibly, stores a number of resulting outputs in variables (or clears/alters such variables)
  • line breaks: whenever the current line ends (or the return key is hit when using the command prompt), Matlab attempts to interpret (translate and act upon) the input language elements
  • space character: delimits elements in an expression (several spaces have the same effect as a single space)
  • semicolon (;): outside of expressions, the semicolon has two main functions, it separates individual statements (mostly useful when several short commands are grouped together) and suppresses the output of a command or statement (normally, if an expression leads to an output, the resulting value/s is/are stored in the default output variable, ans)
  • 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

Comments

This language element is mostly useful when code is not entered into the command line but rather organized in text files for repeated processing of statements.

% this is a comment describing the next statement line
disp('this is the first statement, displaying a line');
 
% this is another comment, describing that two numbers are added
s = a + b;

Control flow elements

When writing code, it is often crucial to either

  • 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)
  • select a (list of) expression/s based on another expression (value)
  • 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)

Conditional code execution

Matlab's principle way of allowing to execute code based on a conditional statement looks as follows:

% open a control flow structure, giving a condition
% upon which the embedded expressions are executed
if my_condition_is_met
    expression1;
    expression2;
    expression3;
end

The conditional expression that is given immediately following the if keyword is executed (and is, in fact, nothing but another expression). It usually is expected that this conditional expression returns a boolean (true or false) value, such that Matlab will then either execute the embedded expressions (if the conditional statement evaluates to true) or not (false). In case the conditional statement does not return a boolean value, Matlab automatically converts the returned value (if possible). In case this conversion fails, an error is thrown.

Conditional expressions can be “linked” (several conditions can be evaluated), by using logic operators (double ampersand and pipe characters):

if (test1 && test2) || (test3 && test4)
    some_function(some_value);
end

Which means that if either both test1 and test2 (these two) or both test3 and test4 result in a true outcome, some_function will be called with the input argument of some_value.

matlab_-_language.1352745043.txt.gz · Last modified: 2012/11/12 19:30 by jochen