A variable ID card

Several weeks ago, I made an analogy between a computer program and a novel. It turned out some of the techniques used to make nice stories are still true when it comes to programming. Here I continue with this analogy and show that variables are like normal people. They have an ID card that is unique. On this path, I answear to this simple question : What is a variable?

Variables are at the heart of any Matlab program. They can store anything that comes to your mind as long as it can be written in text or numbers. At the heart of any variable in Matlab is a matrix. This becomes even clearer if you start interface Matlab with C programming. In C, any Matlab variable is called a mxArray.

A variable is made of several important things which altogether constitutes its unique ID card.

  • First a variable has a name. This name must be unique in memory. A variable name can be a combination of any letter (Matlab consider upper and lower case to be different letters), numbers and underscores. Please note that all variables MUST start with a letter. Surprisingly, Matlab is not as flexible as the US governement when it comes to naming your baby variable: No more than 63 letters.
  • Now that you have a name to your baby. Your baby can be born. Exactly as humans can come in many colors. Variable must have a data type or class. They are many data classes. They are all drawn on the following image from Mathworks:

The most important classes of these classes are the numeric classes. These are subdivided in several floating point and integer classes. Each one of these classes store your number with a different precision (and a different associated memory size). I made a post focused on the numerical classes. The default class for numbers is double. As I said earlier, it is a very conservative choice that fits most needs.

Other interesting classes are struct and cell which I explain entirely in a separated posttext (or char) and function handle. This last one will also requires a dedicated post. It is by far the least known of all the classes.

  • Now that you know the color of your futur baby variable, you need to know its size to get its ID card. Any data class is an array in Matlab. Matlab always know the size of the things it handles. So on your variable ID card is printed whether it is a 3 by 3 array or 2 by 2 by 5.
  • With the size and the baby class in hand, Matlab can now decide on your variable address. Arrays needs to be contiguous block of memory in Matlab. So Matlab takes each class memory needs, multiply by the number of elements, ask the Operating System for a contiguous block of memory of this size and receive the new memory address of your baby variable. This address is stored on your variable ID card as well as the total space it occupies.
  • On many ID cards, there is an addionnal field to indicate some relevant informations, like wearing glasses or contact lenses (this is usefull for Driver ID). Variables have a similar field called “Attributes” which can display a few relevant informations on the scope of the variable (global or persistent if applicable), or whether it is a sparse or complex matrix. I will come back to this field in a dedicated post as well.

To sum up, on any variable ID card, there is :

  • A name
  • A class
  • A size
  • A memory address and its associated size.
  • Optionaly an attribute

Let’s put this new knowledge into practice :

Y=[2 5 12;2 23 5];

To ask for all these variable IDs is easy. Just as when you ask : “who is that?“, just type in :


whos will display the current state of memory like so :

>> whos
Name      Size            Bytes  Class     Attributes

K         1×1                16  double    complex
X         1×1                 8  double
Y         2×3                48  double
Z         1×5                10  char

Variable in Matlab don’t like to show their actual memory address (they are protecting themself a little) but you can force them to display it using :

format debug;

format debug is an undocumented feature that will change your variable display to :

Y =

Structure address = 6da49f8
m = 2
n = 3
pr = 5f8a69d0
pi = 0
2     5    12
2    23     5

Here you have access to the memory address of Y. 6da49f8 is here the address of the object and  5f8a69d0 the address of the real part (pr) of this number. Since Y has no imaginary part, pi is 0.


This entry was posted in Beginners. Bookmark the permalink.

2 Responses to A variable ID card

  1. John says:

    You are doing a nice job here….but would just like you to orderly arrange the topics so that we beginners can know where to start and continue from. Thumbs up Man!!!great work

  2. Moritz says:

    Thanks a lot for your great explanations which make me understand MATLAB so much better!! Please, keep on doing like this! 😀

Leave a Reply to Moritz Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.