matlab_-_datatypes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
matlab_-_datatypes [2012/10/05 18:37] – jochen | matlab_-_datatypes [2012/10/06 04:08] – re-written jochen | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Matlab - datatypes ====== | + | ====== Matlab - variables, |
- | This page covers the basic (built-in) datatypes | + | This page covers |
- | ===== General notes ===== | + | ===== Variables |
- | Variables in Matlab have the following | + | A variable can be thought of as a storage container that has the following properties: |
- | * variables are **accessible via an identifier** that must begin with a letter, may contain numbers and underscores, | + | * links a name (identifier) to a value (or list of values) |
+ | * is available in a workspace, i.e. when a function is called, | ||
+ | * can be assigned a (new) value (or list of values) using the **'' | ||
+ | * has a specific datatype, which can change during the course of a program (or command line session) | ||
+ | * changing datatypes is considered bad coding practice and should be avoided:< | ||
+ | x = 1; | ||
+ | |||
+ | % re-define x as a string: no error! | ||
+ | x = ' | ||
+ | * is of arbitrary size (which means it can also be empty), which can also change (older versions would allow up to 63 dimensions, but this limit no longer exists) | ||
+ | * that means that a variable containing a single number is of the same type as a variable containing several numbers (of the same type) | ||
+ | * does not require declaration (like in C/C++) but can be created at any time (on the command line or at any point in a program, i.e. M-file) | ||
+ | * this unfortunately makes it sometimes difficult to find out what an identifier | ||
+ | | ||
+ | | ||
+ | * to read-access a sub-portion of an array, the index expression has to be provided within parentheses on the right-hand side (e.g. **'' | ||
+ | * on the left-hand side, a part of an array can be replaced with new data (e.g. **'' | ||
+ | * in that case, **'' | ||
+ | * if the variable is smaller than indicated by the index expression, Matlab will attempt to grow the variable accordingly:< | ||
+ | a = [10, 20, 30; 40, 50, 60]; | ||
+ | |||
+ | % assign the value 100 to the second through 4th row and the 3rd through 4th column | ||
+ | a(2:4, 3:4) = 100; | ||
+ | |||
+ | % a is now a 4x4 array!</ | ||
+ | * can be used in expressions (e.g. in computations, | ||
+ | * is available for storing the data contained therein to a file on disk and can be loaded from disk as well | ||
+ | |||
+ | Please note that at the end of a function (including when the keyword **'' | ||
+ | |||
+ | ==== Variable identifiers ==== | ||
+ | There are a few rules applying to identifiers: | ||
+ | * a valid identifier must contain | ||
+ | * it must begin with a letter | ||
+ | * some valid variable names with assignments: | ||
v = 1; | v = 1; | ||
VAR12 = 12; | VAR12 = 12; | ||
A_Really_Long_Name = 'long name';</ | A_Really_Long_Name = 'long name';</ | ||
- | * please note that if a variable is given the same name as an existing function, the identifier then only refers to the variable (see also [[Matlab - precedence|precedence rules]]):< | + | * **keywords can not be used as identifier names**, which excludes the following words from being identifier/ |
+ | * if a variable is given the same name as an existing function, the identifier then only refers to the variable | ||
newarray = [1, 2, 3, 4]; | newarray = [1, 2, 3, 4]; | ||
Line 19: | Line 54: | ||
% then this leads to an error... | % then this leads to an error... | ||
notthesum = sum(newarray); | notthesum = sum(newarray); | ||
- | * variables can be defined (created) at any time and do not require a declaration (such as in C/C++); this unfortunately makes it sometimes difficult to find out what an identifier stands for (function or variable? what datatype and content?) | ||
- | * variables can change type and size at any time in the code (although this is bad coding practice and should be avoided):< | ||
- | x = 1; | ||
- | % re-define x as a string: no error! | + | ===== Datatypes ===== |
- | x = ' | + | Datatypes |
- | * access (availability) of variables is organized in workspaces (different function code files can use the same variable names without conflict) | + | * numeric datatypes |
- | * each variable type supports multidimensional size (older versions would allow up to 63 dimensions, but this limit no longer exists) | + | * text (character/ |
- | * that means that a variable containing a single number is of the same type as a variable containing several numbers | + | * compound datatypes (to store values of different types in one variable) |
- | * to access | + | * function handles |
- | * this sub-portion access also works when only a part of an array is to be replaced with new data (e.g. **'' | + | * user-defined datatypes/ |
- | * in that case, **'' | + | |
- | * if the variable is smaller than indicated by the index expression, Matlab will attempt | + | |
- | a = [10, 20, 30; 40, 50, 60]; | + | |
- | % assign the value 100 to the second through 4th row and the 3rd through 4th column | + | ==== Numeric |
- | a(2:4, 3:4) = 100; | + | |
- | + | ||
- | % a is now a 4x4 array!</ | + | |
- | + | ||
- | ===== Numerical | + | |
By default, a variable in Matlab that is storing a numeric value (or a list/array of numbers) has the datatype " | By default, a variable in Matlab that is storing a numeric value (or a list/array of numbers) has the datatype " | ||
Line 58: | Line 82: | ||
A special case is the **'' | A special case is the **'' | ||
- | ===== Character datatype | + | Please note that instead of being keywords (such as in C/C++), datatypes are not " |
+ | |||
+ | ==== Character datatype ==== | ||
Given that Matlab variables can be arrays of arbitrary size, single characters, as well as " | Given that Matlab variables can be arrays of arbitrary size, single characters, as well as " | ||
Line 74: | Line 100: | ||
% is their distance in the alphabet, in this case 3!</ | % is their distance in the alphabet, in this case 3!</ | ||
- | ===== Cell compound data ===== | + | And that also means that a variable |
- | In many situations it is necessary to store data of different types (e.g. a name/string together with a number, such as age) in a " | + | |
- | To define a cell array as well as to address the **content** of a cell, Matlab uses the "curly braces" | + | ==== Compound datatypes ==== |
+ | In many situations it is necessary to store data of different types (e.g. a name/string together with a number, such as age) in a " | ||
+ | |||
+ | These compound datatypes are further sub-divided into two types: one where elements are (mainly) addressed by a numeric (or equivalent) indices, and one where elements are accessed by a field name in a tree-like structure (same rules as identifiers, | ||
+ | |||
+ | === Cell datatype === | ||
+ | The **'' | ||
+ | |||
+ | To define a cell array as well as to address the **content** of a cell, Matlab uses the "curly braces" | ||
name_and_age = {'John Doe', 41}; | name_and_age = {'John Doe', 41}; | ||
Line 89: | Line 122: | ||
Put differently, | Put differently, | ||
+ | |||
+ | Also, please note that if you use a variable (numeric, char, or compound!) when creating a new compound variable or setting one or several elements of a compound variable, the values in the compound variable will be **copies** of the content of the original variable (or rather, if the variable is altered, a copy is created). For instance:< | ||
+ | n = [12, 14, 10]; | ||
+ | s = ' | ||
+ | |||
+ | % store variables in compound variable | ||
+ | c = {s, n}; | ||
+ | |||
+ | % re-assign index 2 of n variable | ||
+ | n(2) = 18; | ||
+ | |||
+ | % and then c still contains [12, 14, 10] in its second cell element!</ | ||
+ | |||
+ | === Struct datatype === | ||
+ | The other compound datatype in Matlab is the **'' | ||
+ | |||
+ | Syntax-wise, | ||
+ | |||
+ | The obvious advantage is that code usually becomes less cryptic, given that instead of using syntax such as **'' | ||
+ | |||
+ | Please note that variables of type struct still can be of arbitrary size! This means that a list of structures (e.g. 5 people' | ||
+ | this_name = people(3).name; | ||
+ | this_age = people(3).age;</ | ||
+ | |||
+ | In turn this means that if the index expression is omitted, the **'' | ||
+ | all_names = {people.name};</ | ||
+ | |||
+ | === Multi-layered compounding === | ||
+ | Each cell and struct field can contain any of Matlab' | ||
+ | main_tree.leaf(3).subfield{4}(11: | ||
+ | |||
+ | If you see such a piece of code this can be translated into | ||
+ | * main_tree is of datatype struct (and must be of size 1x1, i.e. a scalar struct, to be valid) | ||
+ | * one of the field names of main_tree is called leaf, is also of datatype struct with at least field name subfield, and it presumably has at least 3 elements (see comment below) | ||
+ | * the subfield (of the third leaf!) is of type cell with at least 4 elements | ||
+ | * the 4th element of the subfield is a numeric array | ||
+ | * numeric indices 11 through 20 (of this numeric array) are set to 1 | ||
+ | |||
+ | Please be aware that this line of code is also valid if main_tree is not yet defined! In that case, the **'' | ||
+ | |||
+ | As you can see, while Matlab' | ||
+ | |||
+ | ==== Function handles ==== | ||
+ | The **'' | ||
+ | * the type of operation that is to be applied to a variable (or expression) is not fully determinable before code is run, in which case a function handle can be used to call a variable function (instead of using a syntax construction that selects from all possible options, which still could be insufficient if a function can be user defined) | ||
+ | * an argument in a function call itself is "an operation" | ||
+ | * a function is only available in a certain context (private function or sub-function in an M-file), in which case a function handle can be created and returned, allowing functions outside the original scope to use this function after all | ||
+ | |||
+ | Given the fact that this feature is fairly advanced, I won't be giving any in-depth examples at this point. Just be aware that variables can also be of type **'' | ||
+ | |||
+ | ==== User-defined datatypes ==== | ||
+ | Matlab allows users to add functionality by creating text files with the **'' | ||
+ | |||
+ | For this purpose, Matlab allows to define new datatypes (classes) by adding folders with a leading **'' | ||
+ | |||
+ | Again, this is fairly advanced coding and will not be discussed in-depth at this point. There are, however, a few important aspects I want to mention: | ||
+ | * the internal representation of objects is supposed to be of type struct | ||
+ | * indexing operations (incl. the struct syntax of **'' | ||
+ | * NeuroElf makes use of this feature by allowing a more C++/Visual Basic style syntax:< | ||
+ | % load a VMR into an xff object | ||
+ | vmr = xff(' | ||
+ | |||
+ | % call the function in @xff/ | ||
+ | vmr.Browse; | ||
+ | |||
+ | % get xfigure object of the main UI figure | ||
+ | neuroelf_gui; | ||
+ | global ne_gcfg; | ||
+ | mainfig = ne_gcfg.h.MainFig; | ||
+ | |||
+ | % switch to " | ||
+ | mainfig.ShowPage(2);</ | ||
+ |
matlab_-_datatypes.txt · Last modified: 2012/10/06 15:58 by jochen