Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

2 MATLAB Variables Commands Operators

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

2 MATLAB Variables Commands Operators

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Variables & Commands

Dr. Susovan Jana


Assistant Professor & Assistant HOD
Department of Computer Science & Engineering (IoT)
Institute of Engineering & Management, Kolkata, INDIA
Email (O): susovan.jana@iem.edu.in
Email (P): jana.susovan2@gmail.com
2
Creating a Variable
❑ A variable is simply a name that we assign to a specific value
❑ The newly created variable will be in the matlab workspace
memory until you clear it or quit the matlab application
❑ Syntax
─ <variable name>=<value>;
❑ Example 1:
─ x=7;
❑ Example 2:
─ name="Suvo“;
❑ Example 3: Matrix
─ A=[2 4 7; 3 5 1; 6 7 4];

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
3
Creating a Variable
❑ Semicolon in the end is not
mandatory. If you do not give
semicolon in the end it will print
the variable and value once in
the command window.
❑ Type of variable can be will be
determined by the type of value
assigned to it.
❑ To check the run command:
class(<variable name>)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
4
View Variable Contents

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
5
View Variable Contents

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
6
View Variable Contents

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
7
Rules for Naming a Variable [1/4]
❑ A valid variable name starts with a
letter, followed by letters, digits, or
underscores.
❑ MATLAB is case sensitive, so A and a
are not the same variable.
❑ To get the maximum allowable length
of a variable name in the current
version of MATLAB
─ Run the namelengthmax command

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
8
Rules for Naming a Variable [2/4]
❑ MATLAB keywords can’t be used as
variable name, such as for or end.
❑ To get the complete list of keywords in
your version
─ run the iskeyword command

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
9
Rules for Naming a Variable [3/4]
❑ Examples of valid & invalid variable
name
Examples of valid Examples of invalid
names: names:
x6 6x

lastValue end

n_factorial n!

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
10
Rules for Naming a Variable [4/4]
❑ Conflicts with Function Names
─ Avoid creating variables with the same
name as a function.
─ In general, variable names take
precedence over function names.
─ If you create a variable that uses the name
of a function, you sometimes get
unexpected results.
❑ To check if any variable or function
exists with same name exist in MATLAB
─ exist <proposed name>

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
11
Predefined Variables [1/3]
❑ Predefined variables in MATLAB are not considered
as keywords
❑ We can change their values and use them for our
own purposes
❑ Be very careful when you are using them
❑ Example
─ true

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
12
Predefined Variables [2/3]
Expression Description
pi The number π up to 15 significant digits.
i, j The complex number √−1.
inf Represents the mathematical Infinity concept, for example, a result of division by
zero.
NaN Stands for Not-A-Number. Represents the result of a meaningless mathematical
function, like 0/0.
clock Contains the current date and time in the form of a 6-element row vector: year,
month, day, hour, minute, second.
date Contains a string representing today's date.
eps Stands for epsilon. It represents the smallest number that can be represented by your
MATLAB software.
ans A special variable that MATLAB uses to store the result of MATLAB's command line.

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
13
Predefined Variables [3/3]

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
14
Display Variables
❑ Syntax
─ disp(<variable name>);
❑ Examples
─ disp(x);
─ disp(name);
─ disp(A);

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
15
Editing Variable Contents

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
16
Basic MATLAB Commands
❑ clc
─ clears the command window
❑ clear <variable name>
─ used to remove particular variable from workspace
❑ clear all
─ used to remove all variable from workspace
❑ close all
─ closes all open MATLAB figure windows
❑ quit
─ stops MATLAB
❑ help
─ finding help on particular topic
❑ version
─ Used to know the version of the current matlab

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
17
Basic MATLAB Commands
❑ who
─ shows the list of variables in the current workspace
❑ whos
─ shows the details of the variables in the current workspace

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
18
Basic MATLAB Commands
❑ date
─ display the current date of the system
❑ delete
─ delete a file from system
❑ cd
─ gives the current directory/used to change the directory
❑ dir
─ gives the list of files in the current directory
❑ save
─ save workspace variables into a .mat file
─ save <file name>.mat
❑ load
─ load the variables from a file into workspace
─ load (‘<file name>.mat ’)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
19
Input / Output commands
❑ disp
─ display the contents of a variable
❑ input
─ accepts inputs from command window

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
20
Input / Output commands
❑ fscanf
─ read formatted data from a file
❑ fprintf
─ performs formatted write on screen or file

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
21
Some format code & it’s uses
Format Code Purpose
%s Format as a string
%d Format as an integer
%f Format as a floating point value
\n Insert a new line in the output string
\t Insert a tab in the output string

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
22
Arithmetic operators in MATLAB
Symbol Role Alternative

+ Addition plus()

+ Unary plus uplus()

- Subtraction minus()

- Unary minus uminus()

.* Element-wise multiplication times()

* Matrix multiplication mtimes()

./ Element-wise right division rdivide()

/ Matrix right division mrdivide()

.\ Element-wise left division ldivide()

\ Matrix left divison (also known as backslash) mldivide()

.^ Element-wise power power()

^ Matrix power mpower()

.' Transpose transpose()

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
23
Relational Operators in MATLAB
Symbol Role Alternative
== Equal to eq()
~= Not equal to ne()
> Greater than gt()
>= Greater than or equal to ge()
< Less than lt()
<= Less than or equal to le()

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
24
Logical operators in MATLAB
Symbol Role Alternative
& Logical AND and()
| Logical OR or()
~ Logical NOT not()

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
25

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA

You might also like