MATLAB Variables

What are variables?

A variable is a storage location or a container to store or hold data. In programming, variables are used to reservedata created by the programmers so that it can be used later. Variable points a logical address in the memory of a system to storedifferent type of data. Suppose you want to store a value ‘10’ in your computer’s memory for further use. Then you would create a variable by giving any name and assign that value to this name which you have created. The below illustration can help to understand how a variable is implemented.

MATLAB Variables

Every variable created in MATLAB environment, is a matrix or array. An array is a structural unit of data and a collection of values stored in consecutive memory location.

MATLAB Variables

The above statement gets executed by creating a 1-by-1 matrix named p and 87 value is stored.

In MATLAB, all the variables created during a session, are stored in MATLAB workspace.

MATLAB Variables

Initializing and creating variables

The following methods are used to create and initialize variables.

MATLAB Variables

Variable naming conventions

As MATLAB is case sensitive so few rules to be followed while creating variable names

àIn MATLAB, s and S are different

àName should begin with a letter or followed by underscores, digits.

àMATLAB keywords should be avoided as variable name.

àMATLAB functions name should not conflict with variable names.

àVariables names should not be any of the MATLAB’S keywords like end, case, break, for, continue, global, function, catch, switch, parfor, persistent, return, if, while, try, spmd.

Example of valid variables:

  • x7
    • firstvalue
    • i_name

Examples of invalid variables names:

  • 7x
    • n!
    • end

Important note while creating variables:

  1. We can refer any variable at any time once a variable is created and entered in the memory of the system.
  2. Before we use any variable in our program, it must be assigned a value to it.
  3. By default, “ans” variable is created by MATLAB’s environment to store the result of any expression executed.
  4. We can edit the value of any variable by assigning the new value again to it.
  5. To check the variables that we have created we can look into the screen of workspace or use “who” and “whos” commands.
  6. We can resize or reshape the variables that means to modify the order of elements in the variable.

MULTIPLE ASSIGNMENTS

Multiple assignments of variable is supported. In MATLAB, we can assign multiple variables on the same line.

x = 7;  b = 8;  h = x*b

The following output is resulted by executing the above statement.

h = 56

How to display all variables at a time?

To display all variables used in the current project we use the command who

Who

The following output is resulted by executing the above statement.

Your variables are: x    ans   y   z  

Similarly, the command whos displays mores information about the variables.

Whos

The following output is resulted by executing the above statement.

Attr name      size      bytes    class ========      =====    ====  ===== x                   1x1           8           double ans              1x70            675        cell y                   1x1              8          double z                   1x1              8          double  
MATLAB Variables

Assigning vectors to variable

A vector is a array of numbers( one-dimensional array). In MATLAB environment, every variable is considered as an array or matrix.

Types:

àrow vectors

 p = [2 3 4 5 6 7] 

The following output is resulted by executing the above statement.

 p=           2  3  4  5   6   7 

àcolumn vectors

 i = [5; 6; 7; 8] 

The following output is resulted by executing the above statement.

 i =          5          6          7          8 

Creating Matrices and storing in a variable

Every variable is treated as an array or matrix, so let’s see how to create a matrix

Matrix is a 2D array of numbers. We can apply many functions to a matrix like transpose, addition of matrices, multiplication. In MATLAB, matrix is created with the help of array using semicolons (;) as shown below.

Example 1:

 >>zeros(2,2)
 ans =
      0     0
      0     0
 >> a = zeros(1,3)
 a =
      0     0     0 

Example 2:

Matrix1=[1 2 3; 4 5 6; 7 8 9]  

The following output is resulted by executing the above statement on Matlab’s command line.

Matrix1=                   1   2   3                    4   5   6                   7   8   9
Concatenation functions of matrix
  • cat=along with the dimensions specified matrices are concatenated.
  • blkdiag=from a existing matrix, a block diagonal matrix is created
  • repmat=by tiling and replicating a new matrix is created
  • horzcat=matrices are concatenated horizontally.
  • vertcat= matrices are concatenated vertically.

Examples1:

We have performed matrix concatenation by using cat() function on p and q matrices.We have executed this on Matlab’s command line on

 p = ones(1,4);
 q = zeros(1,4);
 c = cat(p,q)
 c = 1×8
      1     1     1     1     0     0     0     0 

Example2:

In this example, we are applying blkdiag() on matrices A1,A2,A3 as shown below. We have executed this on Matlab’s command line

 A1 = ones(3,2);
 A2 = 9*ones(3,2);
 A3 = 7*ones(3,3);
 B = blkdiag(A1,A2,A3)
 
B =

     1     1     0     0     0     0     0
     1     1     0     0     0     0     0
     1     1     0     0     0     0     0
     0     0     9     9     0     0     0
     0     0     9     9     0     0     0
     0     0     9     9     0     0     0
     0     0     0     0     7     7     7
     0     0     0     0     7     7     7
     0     0     0     0     7     7     7 

Example3:

 D = horzcat(p,q)
 
D =

     1     1     1     1     0     0     0     0 

In the above example, p and q are taken from example1 to perform horizontal concatenation.

We have executed this on Matlab’s command line.



ADVERTISEMENT
ADVERTISEMENT