Saturday, December 26, 2009

2) Data Types

Hi,
In the last post, we looked at how to start matlab and did our first program in matlab.

Now that we can start and use matlab, lets start with first of our topics.
"Data Types".

        Please however note that this is just a compilation of few of the functionalities that matlab possesses. More details about capabilities of Matlab can be found at the user manual.  
       Starting on the data types, actually one doesnt need to worry too much about data types in matlab.You dont have to specify data types explicitely for most of your work in matlab.
But, this is not similar to any scripting langauge where you can use same variable interchangeably across program without bothering about the data type. In matlab, you do need to worry about data types, its just that for most of the times you dont have to explicitely set them as we do in C or C++.

         Following figure gives Idea about how matlabs data types are organised.
This figure has been taken from the official user manual of Matlab.



  So as shown by this figure, basic data types in matlab can primarily be divided 
5 categories or classes.
  1. Boolean
  2. Numeric
  3. Text
  4. Function Handle
  5. Heterogeneous Container.
          There is one more data type Map which is essentially a hashtable. We will look at Function Handles and the maps later in advance topics. For now lets focus on the 4 basic types.
But before we do that, lets have a look at one very useful command in matlab whos.
           This command displays all variables present in the workspace in current scope. With listing all the variables, it also lists there data type as well. Here is an example.

>> a = rand(3,4);
>> b = uint8(34);
>> c= 300.234234;
>> whos
  Name      Size                    Bytes  Class

  a         3x4                        96  double array
  b         1x1                         1  uint8 array
  c         1x1                         8  double array

Grand total is 14 elements using 105 bytes


 
              See the different data types listed in the end of each line.

Remember that default data type of matlab is double. So if you do not specify the data type, it will be treated as double or double array to be precise. Lets not worry about this double array too much at this point or in fact anypoint later as well. Here is an example of default assignment.
>> b = 90
b =
    90

>> whos
  Name      Size                    Bytes  Class

  b         1x1                         8  double array

Grand total is 1 element using 8 bytes


              Even when we assign a pure integer value to b, default of double has been assigned.
While this doesnot affect operations in most of the cases, we have to be careful sometimes 
while writing array as an image where integers are required. Also, note that after I assigned 90 to b there is one more line printed there right after I hit enter.This is because I didnot give a semicolon after the statement. If you dont give a semicolon after your statement, final variable getting modified by that statement will be printed on the screen.While this is a very useful functionality at debug time, can be extremely time consuming if you dont give a semicolon for a statement in the for,while loops where matlab has to print the values on the screen every time statement gets executed.
             We will look at two more commands here before we proceed with data types. clc is a command to clear contents of the screen just like familiar "clear" command on terminal. Another command is clear. This is a command used to clear contents of the memory. Be extremely careful while using this command. 

>> a = rand(3,4);
>> v = 23;      
>> p = int32(2321312);
>> whos
  Name      Size                    Bytes  Class

  a         3x4                        96  double array
  p         1x1                         4  int32 array
  v         1x1                         8  double array

Grand total is 14 elements using 108 bytes



Now see what happens after the clear command.



>> clear
>> whos
>>




All variables are gone....!!! and gone permanently. So be extremely careful while using this command.
This command can also be used selectively. You can specify which variable you want to erase.

>> a = rand(3,4);    
>> v = 23;           
>> p = int32(2321312);

>> whos
  Name      Size                    Bytes  Class

  a         3x4                        96  double array
  p         1x1                         4  int32 array
  v         1x1                         8  double array

Grand total is 14 elements using 108 bytes

>> clear a p;        




>> whos
  Name      Size                    Bytes  Class

  v         1x1                         8  double array



See that only variables a and p were cleared from the memory.
This use comes in handy when you have to load a file into the memory and dont want to use all of its contents. You can remove them from the memory.


Now that we know these basic commands, in coming posts, lets start looking at individual data types one by one.


~Omkar



No comments:

Post a Comment