User Tools

Site Tools


matlab_-_datatypes

This is an old revision of the document!


Matlab - variables, datatypes, and indexing

This page covers the use of variables in Matlab, the basic (built-in) datatypes, together with some information on how they can be used, how they can be converted, and how they relate to one another. The last part covers the different indexing syntax mechanisms and some pitfalls.

Variables

A variable can be thought of as a storage container that has the following properties:

  • links a name (identifier) to a value (or list of values)
  • is available in a workspace, i.e. when a function is called, variables available in the calling workspace are hidden, unless they are global variables (different function code files can use the same variable names without conflict)
  • can be assigned a (new) value (or list of values) using the = sign (assignment operator)
  • 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:
      % define x as a number
      x = 1;
       
      % re-define x as a string: no error!
      x = 'string';
  • 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 stands for (function or variable? what datatype and content?)
  • stores this value until it is reassigned a new value or the variable is “clear-ed” from the workspace
  • allows indexing operations to access sub-parts of a list of values (both reading from and writing into parts of variables)
    • 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. part = fullarray(portion);)
    • on the left-hand side, a part of an array can be replaced with new data (e.g. fullarray(portion) = newvalues;)
    • in that case, newvalues must either be a single number (all indices addressed by portion will be set to the same number) or must match in size
    • if the variable is smaller than indicated by the index expression, Matlab will attempt to grow the variable accordingly:
      % define a 2x3 array
      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, function calls, to index another variable, or to form new, compound variables)
  • 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 return is reached while executing a function) all variables that are not “returned” or marked as persistent are cleared from the workspace and memory.

Variable identifiers

There are a few rules applying to identifiers:

  • a valid identifier must contain only letters (lower and upper case), numbers, and underscores, but no symbols or blanks
  • it must begin with a letter
    • some valid variable names with assignments:
      v = 1;
      VAR12 = 12;
      A_Really_Long_Name = 'long name';
  • keywords can not be used as identifier names, which excludes the following words from being identifier/variable names: break, case, catch, classdef, continue, else, elseif, end, for, function, global, if, otherwise, parfor, persistent, return, spmd, switch, try, while
  • if a variable is given the same name as an existing function, the identifier then only refers to the variable in this workspace (see also precedence rules):
    % defining a new array
    newarray = [1, 2, 3, 4];
     
    % computing the sum
    sumarray = sum(newarray);
     
    % redefining sum
    sum = 5;
     
    % then this leads to an error...
    notthesum = sum(newarray); % index exceeds dimensions!!

Datatypes

Datatypes can be, generally, divided into 5 major groups:

  • numeric datatypes (incl. a logical datatype for true/false values)
  • text (character/string) datatype
  • compound datatypes (to store values of different types in one variable)
  • function handles
  • user-defined datatypes/objects

Numeric datatypes

By default, a variable in Matlab that is storing a numeric value (or a list/array of numbers) has the datatype “double”. So, in a simple assignment of a number (or array) to a variable (such as a = 1; or b = [2, 3, 4];), the datatype would be double, regardless of whether the number is integer or not! While this requires more memory (for large arrays of numbers), at least the user (or code writer) doesn't have to worry about datatype conversions, etc. Unless you have very specific needs (e.g. lower memory usage or increased speed for specific operations), it is recommended to use the default datatype.

Numeric variables are defined by simply assigning the output of a function that returns a number to a variable or by setting the value(s) manually.

Here is a list of all “numeric” datatypes:

  • double (default type for all numbers, supports decimal numbers/fractions): each value being stored requires 8 byte (= 64 bits) of memory, 1 bit for the sign, 11 bits for the exponent, and 52 bits for a base-2 fraction (see Double precision floating point format at wikipedia). Special “configurations” are used to store the values Inf, -Inf, and NaN.
  • single: each value being stored requires 4 byte (= 32 bits) of memory; in short, the datatype has similar properties compared to double, just less “precision” (and exponent range)
  • int64: a 64-bit (8-byte) integer datatype (signed); lowest value is -2^63, highest value is 2^63 - 1
  • uint64: a 64-bit (8-byte) integer datatype (unsigned); lowest value 0, highest value is 2^64 - 1
  • int32: a 32-bit (4-byte) integer datatype (signed); lowest value is -2^31, highest value is 2^31 - 1
  • uint32: a 32-bit (4-byte) integer datatype (unsigned); lowest value is 0, highest value is 2^32 - 1
  • int16: a 16-bit (2-byte) integer datatype (signed); lowest value is -32768, highest value is 32767
  • uint16: a 16-bit (2-byte) integer datatype (unsigned); lowest value is 0, highest value is 65535
  • int8: an 8-bit (1-byte) integer datatype (signed); lowest value is -128, highest value is 127
  • uint8: an 8-bit (1-byte) integer datatype (signed); lowest value is -128, highest value is 127

A special case is the logical datatype. It can only store two values: false or true. If converted to any of the other numeric datatypes, false is converted to 0 and true is converted to 1.

Character datatype

Given that Matlab variables can be arrays of arbitrary size, single characters, as well as “strings” (a series of characters, such as in a word or sentence) and also lists of strings (two-dimensional field of characters) all are stored with the same basic datatype: char.

Here are some examples defining variables of type char:

letter = 'x';
start = 'The letter is';
sentence = [start, ' ', letter, '.'];

The resulting variable then contains the string “The letter is x.”.

Importantly, the underlying storage is yet a numeric datatype:

% the difference between to characters
'd' - 'a'
 
% is their distance in the alphabet, in this case 3!

Cell compound data

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 “dataset”, which still should be accessible via a single variable. For this purpose, Matlab provides the cell datatype.

To define a cell array as well as to address the content of a cell, Matlab uses the “curly braces” characters: { and }:

% define a 1x2 cell array with a name and an age
name_and_age = {'John Doe', 41};
 
% to access just the name we index the first cell with {1}
name = name_and_age{1};
 
% and for the age the second cell
age = name_and_age{2};

Please be aware that a cell array can, naturally, also be indexed with the parentheses syntax. However, in that case the returned value will be of type cell. In fact, every index expression on a variable of a built-in datatype using the parentheses syntax always returns a value (or values) of the same datatype!

Put differently, if you imagine a shelf with 5 jars on it. The entire shelf then represents a cell array (by the name of shelf). The expression shelf(3) will then return the third jar of the shelf. On the other hand, the expression shelf{3} returns the content of the third jar!

matlab_-_datatypes.1349491430.txt.gz · Last modified: 2012/10/06 04:43 by jochen