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.


Related Topics

MATLAB vs Other Languages

MATLAB vs Other Languages If we try to find which is the best computer programming languages, the answer is not straightforward, and that might sound similar to asking which is the...

3 minutes read.

Matlab function

Introduction: Functions in MATLAB are composed with code that link one variable with another, and yielded output is connected precisely to one specific information that shapes a significant piece of any...

6 minutes read.

Matlab Square Wave Signal Generation

What is Square Wave signal Generator? A “square wave” is a sort of non-sinusoidal waveform, most normally experienced in gadgets and sign preparing. An optimal square wave substitutes consistently and momentarily...

3 minutes read.

MATLAB Operators

What is an Operator? A specific operation is performed by using a symbol which is called an operator. Example: A+B, where A, B are called operand and '+' is called operator. If you...

8 minutes read.

Matlab Matrix

Matlab Matrix What is a Matrix? a rectangular array of expressions arranged in particular rows and columns that is treated as a solitary substance and controlled by specific standards. MatlabMatrix A Framework called matrix,...

5 minutes read.

Matlab Simulink

What is Simulink? Simulink is a re-enactment and model-based plan climate for dynamic and installed frameworks, incorporated with MATLAB. Simulink, additionally created by "MathWorks", is an information stream graphical programming language apparatus...

7 minutes read.

Differentiation in Matlab

Differentiation in Mathematics In maths, Derivatives are an essential device of math or calculus. For instance, derivative of the situation of a moving article regarding time is the item's speed: this...

6 minutes read.

MATLAB Setup And Environment

MATLAB Setup And Environment MATLAB System Requirements As we have already discussed the prerequisite to begin the learning journey of MATLAB now let us drive into some technical system requirements. RAMàMinimum 4GB or...

3 minutes read.

MATLAB Gaussian Pulse

Gaussian pulse shape description: The diagram of a Gaussian is a trademark symmetric "ringer bend" shape. The boundary an is the stature of the bend's pinnacle, b is the situation of...

3 minutes read.

Matlab Calculus

Analytics Calculus is significant in various fields like designing, physical science, and different sciences, yet the estimations are finished by machines or PCs, regardless of whether they are into Physics...

3 minutes read.

MATLAB Commands

MATLAB Commands MATLAB (stands for matrix laboratory) is a high-level programming environment for scientific calculation, and it provides functions more efficiently than other programming languages. In addition, it has many in-build...

3 minutes read.

matlab polynomials

In mathematics, a polynomial is an articulation comprising of indeterminates factors and the respective coefficients, that includes just the tasks of augmentation, expansion, deduction, and non-negative whole number exponentiation of...

4 minutes read.

MATLAB Program - To Solve Differential Equation Using Euler’s Method

Using Euler's method to solve differential equation Can you imagine a point where we do not use differential equation to solve differential condition? Truth be told, there are a few different...

3 minutes read.

MATLAB Data types

What is Data Type? A data type is a classification that helps to decide what class of value a variable can reserve in programming. And also, what sort of operations can...

6 minutes read.

MATLAB Simulink examples

We will discuss few examples to understand different use-cases of MATLAB’s Simulink. Simulink is basically MATLAB-based. Its essential interface is a graphical square charting apparatus and an adaptable arrangement of square...

4 minutes read.

MATLAB Butterworth filter

What is Butterworth filter? The Butterworth channel is a kind of sign preparing channel intended to have a recurrence reaction as level as conceivable in the passband. It is likewise alluded...

3 minutes read.

MATLAB Strings

You can address text in MATLAB utilizing string exhibits. Every component of a string exhibit stores a grouping of characters. The arrangements can have various lengths without cushioning, for example,...

5 minutes read.

Matlab Unit impulse signal generation

What is the Necessity of learning Matlab programs? Matlab Programming Examples give you an idea of typical Matlab programs in a nutshell.Matlab programming should be utilizing object-oriented programming, GUI programming, and...

3 minutes read.

MATLAB Scripts

Scripts Introduction Matlab Script represents some programs and is executed like we execute command in the command window of Matlab’s environment. Matlab script is also defined as a sequence of commands;...

6 minutes read.

MATLAB Transform

MATLAB furnishes for working with changes, for example, the Laplace and Fourier changes. Changes are utilized in science and designing as an instrument for improving on investigation and take a...

5 minutes read.