SlideShare a Scribd company logo
http://www.tutorialspoint.com/matlab/matlab_variables.htm Copyright © tutorialspoint.com
MATLAB - VARIABLES
InMATLAB environment, every variable is anarray or matrix.
Youcanassignvariables ina simple way. For example,
x = 3 % defining x and initializing it with a value
MATLAB willexecute the above statement and returnthe following result:
x =
3
It creates a 1-by-1 matrix named x and stores the value 3 inits element. Let us check another example,
x = sqrt(16) % defining x and initializing it with an expression
MATLAB willexecute the above statement and returnthe following result:
x =
4
Please note that:
Once a variable is entered into the system, youcanrefer to it later.
Variables must have values before they are used.
Whenanexpressionreturns a result that is not assigned to any variable, the systemassigns it to a variable
named ans, whichcanbe used later.
For example,
sqrt(78)
MATLAB willexecute the above statement and returnthe following result:
ans =
8.8318
Youcanuse this variable ans:
9876/ans
MATLAB willexecute the above statement and returnthe following result:
ans =
1.1182e+03
Let's look at another example:
x = 7 * 8;
y = x * 7.89
MATLAB willexecute the above statement and returnthe following result:
y =
441.8400
Multiple Assignments
Youcanhave multiple assignments onthe same line. For example,
a = 2; b = 7; c = a * b
MATLAB willexecute the above statement and returnthe following result:
c =
14
I have forgotten the Variables!
The who command displays allthe variable names youhave used.
who
MATLAB willexecute the above statement and returnthe following result:
Your variables are:
a ans b c x y
The whos command displays little more about the variables:
Variables currently inmemory
Type of eachvariables
Memory allocated to eachvariable
Whether they are complex variables or not
whos
MATLAB willexecute the above statement and returnthe following result:
Name Size Bytes Class Attributes
a 1x1 8 double
ans 1x1 8 double
b 1x1 8 double
c 1x1 8 double
x 1x1 8 double
y 1x1 8 double
The clear command deletes all(or the specified) variable(s) fromthe memory.
clear x % it will delete x, won't display anything
clear % it will delete all variables in the workspace
% peacefully and unobtrusively
Long Assignments
Long assignments canbe extended to another line by using anellipses (...). For example,
initial_velocity = 0;
acceleration = 9.8;
time = 20;
final_velocity = initial_velocity ...
+ acceleration * time
MATLAB willexecute the above statement and returnthe following result:
final_velocity =
196
The format Command
By default, MATLAB displays numbers withfour decimalplace values. This is knownas short format.
However, if youwant more precision, youneed to use the format command.
The format long command displays 16 digits after decimal.
For example:
format long
x = 7 + 10/3 + 5 ^ 1.2
MATLAB willexecute the above statement and returnthe following result:
x =
17.231981640639408
Another example,
format short
x = 7 + 10/3 + 5 ^ 1.2
MATLAB willexecute the above statement and returnthe following result:
x =
17.2320
The format bank command rounds numbers to two decimalplaces. For example,
format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6
MATLAB willexecute the above statement and returnthe following result:
weekly_wage =
1064.70
MATLAB displays large numbers using exponentialnotation.
The format short e command allows displaying inexponentialformwithfour decimalplaces plus the exponent.
For example,
format short e
4.678 * 4.9
MATLAB willexecute the above statement and returnthe following result:
ans =
2.2922e+01
The format long e command allows displaying inexponentialformwithfour decimalplaces plus the exponent.
For example,
format long e
x = pi
MATLAB willexecute the above statement and returnthe following result:
x =
3.141592653589793e+00
The format rat command gives the closest rationalexpressionresulting froma calculation. For example,
format rat
4.678 * 4.9
MATLAB willexecute the above statement and returnthe following result:
ans =
2063/90
Creating Vectors
A vector is a one-dimensionalarray of numbers. MATLAB allows creating two types of vectors:
Row vectors
Columnvectors
Row vectors are created by enclosing the set of elements insquare brackets, using space or comma to
delimit the elements.
For example,
r = [7 8 9 10 11]
MATLAB willexecute the above statement and returnthe following result:
r =
Columns 1 through 4
7 8 9 10
Column 5
11
Another example,
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
MATLAB willexecute the above statement and returnthe following result:
res =
Columns 1 through 4
9 11 13 15
Column 5
17
Column vectors are created by enclosing the set of elements insquare brackets, using semicolon(;) to delimit
the elements.
c = [7; 8; 9; 10; 11]
MATLAB willexecute the above statement and returnthe following result:
c =
7
8
9
10
11
Creating Matrices
A matrix is a two-dimensionalarray of numbers.
InMATLAB, a matrix is created by entering eachrow as a sequence of space or comma separated elements,
and end of a row is demarcated by a semicolon. For example, let us create a 3-by-3 matrix as:
m = [1 2 3; 4 5 6; 7 8 9]
MATLAB willexecute the above statement and returnthe following result:
m =
1 2 3
4 5 6
7 8 9

More Related Content

PDF
Matlab cheatsheet
PDF
Matlab matrics
PPTX
Lecture two
PDF
Matlab booklet
PPTX
Lecture one
PDF
Matlab cheatsheet
PPTX
Lecture three
Matlab cheatsheet
Matlab matrics
Lecture two
Matlab booklet
Lecture one
Matlab cheatsheet
Lecture three

What's hot (20)

PDF
QUESTION 3 Write a program called Triangle.java which first reads in the (x,y...
PPTX
PDF
Matlab tutorial 1
PDF
Matlabch01
PDF
Integration
PDF
Travaux Pratique Matlab + Corrige_Smee Kaem Chann
PDF
Matlab ch1 intro
PDF
BPstudy sklearn 20180925
DOC
Matlabtut1
PPTX
PDF
Matlab practice
PDF
I need some help implementing this in java using net beans. can anyone help ...
PPT
Applications of numerical methods
PPTX
Intro to Matlab programming
PPT
Scilab for real dummies j.heikell - part 2
DOC
Lesson 3
PPTX
Lectue five
PDF
MATLAB for Technical Computing
PPTX
Introduction to MATLAB
QUESTION 3 Write a program called Triangle.java which first reads in the (x,y...
Matlab tutorial 1
Matlabch01
Integration
Travaux Pratique Matlab + Corrige_Smee Kaem Chann
Matlab ch1 intro
BPstudy sklearn 20180925
Matlabtut1
Matlab practice
I need some help implementing this in java using net beans. can anyone help ...
Applications of numerical methods
Intro to Matlab programming
Scilab for real dummies j.heikell - part 2
Lesson 3
Lectue five
MATLAB for Technical Computing
Introduction to MATLAB
Ad

Viewers also liked (7)

PDF
Mit6 094 iap10_lec01
PPTX
graphs plotting in MATLAB
PPTX
D.c. machine
PPTX
Variables in matlab
PPT
Loops in matlab
PDF
Introduction to Matlab
PPTX
Matlab ppt
Mit6 094 iap10_lec01
graphs plotting in MATLAB
D.c. machine
Variables in matlab
Loops in matlab
Introduction to Matlab
Matlab ppt
Ad

Similar to Matlab variables (20)

PDF
Matlab guide
PPTX
From zero to MATLAB hero: Mastering the basics and beyond
PPTX
An Introduction to MATLAB for beginners
PDF
PPT
Brief Introduction to Matlab
PDF
MATLAB INTRODUCTION
PDF
Lecture 01 variables scripts and operations
PDF
lecture #1 & 2....education....matlab...helpful
PPTX
Matlab ppt
PPTX
MATLAB for Engineers ME1006 (1 for beginer).pptx
PDF
Matlab Tutorial for Beginners - I
PDF
Programming with matlab session 1
PPTX
Introduction to scientific computing with matlab.pptx
PPT
Matlab Introduction for beginners_i .ppt
PPT
Matlab_Introduction_by_Michael_Medvinsky.ppt
PPT
MatlabIntroduction presentation xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PDF
MATLAB_Introduction RS&GIS training geomatics
PDF
Basic matlab for beginners
PDF
A complete introduction on matlab and matlab's projects
PPTX
intro2matlab-basic knowledge about Matlab.pptx
Matlab guide
From zero to MATLAB hero: Mastering the basics and beyond
An Introduction to MATLAB for beginners
Brief Introduction to Matlab
MATLAB INTRODUCTION
Lecture 01 variables scripts and operations
lecture #1 & 2....education....matlab...helpful
Matlab ppt
MATLAB for Engineers ME1006 (1 for beginer).pptx
Matlab Tutorial for Beginners - I
Programming with matlab session 1
Introduction to scientific computing with matlab.pptx
Matlab Introduction for beginners_i .ppt
Matlab_Introduction_by_Michael_Medvinsky.ppt
MatlabIntroduction presentation xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
MATLAB_Introduction RS&GIS training geomatics
Basic matlab for beginners
A complete introduction on matlab and matlab's projects
intro2matlab-basic knowledge about Matlab.pptx

More from pramodkumar1804 (20)

PDF
Matlab syntax
PDF
Matlab strings
PDF
Matlab simulink
PDF
Matlab polynomials
PDF
Matlab plotting
PDF
Matlab overview 3
PDF
Matlab overview 2
PDF
Matlab overview
PDF
Matlab operators
PDF
Matlab numbers
PDF
Matlab m files
PDF
Matlab loops 2
PDF
Matlab loops
PDF
Matlab integration
PDF
Matlab graphics
PDF
Matlab gnu octave
PDF
Matlab operators
PDF
Matlab functions
PDF
Matlab differential
PDF
Matlab decisions
Matlab syntax
Matlab strings
Matlab simulink
Matlab polynomials
Matlab plotting
Matlab overview 3
Matlab overview 2
Matlab overview
Matlab operators
Matlab numbers
Matlab m files
Matlab loops 2
Matlab loops
Matlab integration
Matlab graphics
Matlab gnu octave
Matlab operators
Matlab functions
Matlab differential
Matlab decisions

Recently uploaded (20)

PPTX
Presentation on HIE in infants and its manifestations
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharma ospi slides which help in ospi learning
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
master seminar digital applications in india
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Presentation on HIE in infants and its manifestations
2.FourierTransform-ShortQuestionswithAnswers.pdf
O7-L3 Supply Chain Operations - ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
Complications of Minimal Access Surgery at WLH
Pharma ospi slides which help in ospi learning
A systematic review of self-coping strategies used by university students to ...
Chinmaya Tiranga quiz Grand Finale.pdf
Anesthesia in Laparoscopic Surgery in India
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
master seminar digital applications in india
Microbial diseases, their pathogenesis and prophylaxis
102 student loan defaulters named and shamed – Is someone you know on the list?

Matlab variables

  • 1. http://www.tutorialspoint.com/matlab/matlab_variables.htm Copyright © tutorialspoint.com MATLAB - VARIABLES InMATLAB environment, every variable is anarray or matrix. Youcanassignvariables ina simple way. For example, x = 3 % defining x and initializing it with a value MATLAB willexecute the above statement and returnthe following result: x = 3 It creates a 1-by-1 matrix named x and stores the value 3 inits element. Let us check another example, x = sqrt(16) % defining x and initializing it with an expression MATLAB willexecute the above statement and returnthe following result: x = 4 Please note that: Once a variable is entered into the system, youcanrefer to it later. Variables must have values before they are used. Whenanexpressionreturns a result that is not assigned to any variable, the systemassigns it to a variable named ans, whichcanbe used later. For example, sqrt(78) MATLAB willexecute the above statement and returnthe following result: ans = 8.8318 Youcanuse this variable ans: 9876/ans MATLAB willexecute the above statement and returnthe following result: ans = 1.1182e+03 Let's look at another example: x = 7 * 8; y = x * 7.89 MATLAB willexecute the above statement and returnthe following result: y = 441.8400
  • 2. Multiple Assignments Youcanhave multiple assignments onthe same line. For example, a = 2; b = 7; c = a * b MATLAB willexecute the above statement and returnthe following result: c = 14 I have forgotten the Variables! The who command displays allthe variable names youhave used. who MATLAB willexecute the above statement and returnthe following result: Your variables are: a ans b c x y The whos command displays little more about the variables: Variables currently inmemory Type of eachvariables Memory allocated to eachvariable Whether they are complex variables or not whos MATLAB willexecute the above statement and returnthe following result: Name Size Bytes Class Attributes a 1x1 8 double ans 1x1 8 double b 1x1 8 double c 1x1 8 double x 1x1 8 double y 1x1 8 double The clear command deletes all(or the specified) variable(s) fromthe memory. clear x % it will delete x, won't display anything clear % it will delete all variables in the workspace % peacefully and unobtrusively Long Assignments Long assignments canbe extended to another line by using anellipses (...). For example, initial_velocity = 0; acceleration = 9.8; time = 20; final_velocity = initial_velocity ... + acceleration * time MATLAB willexecute the above statement and returnthe following result:
  • 3. final_velocity = 196 The format Command By default, MATLAB displays numbers withfour decimalplace values. This is knownas short format. However, if youwant more precision, youneed to use the format command. The format long command displays 16 digits after decimal. For example: format long x = 7 + 10/3 + 5 ^ 1.2 MATLAB willexecute the above statement and returnthe following result: x = 17.231981640639408 Another example, format short x = 7 + 10/3 + 5 ^ 1.2 MATLAB willexecute the above statement and returnthe following result: x = 17.2320 The format bank command rounds numbers to two decimalplaces. For example, format bank daily_wage = 177.45; weekly_wage = daily_wage * 6 MATLAB willexecute the above statement and returnthe following result: weekly_wage = 1064.70 MATLAB displays large numbers using exponentialnotation. The format short e command allows displaying inexponentialformwithfour decimalplaces plus the exponent. For example, format short e 4.678 * 4.9 MATLAB willexecute the above statement and returnthe following result: ans = 2.2922e+01 The format long e command allows displaying inexponentialformwithfour decimalplaces plus the exponent. For example, format long e x = pi
  • 4. MATLAB willexecute the above statement and returnthe following result: x = 3.141592653589793e+00 The format rat command gives the closest rationalexpressionresulting froma calculation. For example, format rat 4.678 * 4.9 MATLAB willexecute the above statement and returnthe following result: ans = 2063/90 Creating Vectors A vector is a one-dimensionalarray of numbers. MATLAB allows creating two types of vectors: Row vectors Columnvectors Row vectors are created by enclosing the set of elements insquare brackets, using space or comma to delimit the elements. For example, r = [7 8 9 10 11] MATLAB willexecute the above statement and returnthe following result: r = Columns 1 through 4 7 8 9 10 Column 5 11 Another example, r = [7 8 9 10 11]; t = [2, 3, 4, 5, 6]; res = r + t MATLAB willexecute the above statement and returnthe following result: res = Columns 1 through 4 9 11 13 15 Column 5 17 Column vectors are created by enclosing the set of elements insquare brackets, using semicolon(;) to delimit the elements. c = [7; 8; 9; 10; 11] MATLAB willexecute the above statement and returnthe following result: c = 7 8 9
  • 5. 10 11 Creating Matrices A matrix is a two-dimensionalarray of numbers. InMATLAB, a matrix is created by entering eachrow as a sequence of space or comma separated elements, and end of a row is demarcated by a semicolon. For example, let us create a 3-by-3 matrix as: m = [1 2 3; 4 5 6; 7 8 9] MATLAB willexecute the above statement and returnthe following result: m = 1 2 3 4 5 6 7 8 9