You are on page 1of 31

Computer Applications in

Engineering Design

LECTURE 2
M FILES & VISUALIZING DATA IN MATLAB

asim.raheel@uettaxila.edu.pk

Engr. Aasim Raheel


Flashback
➢Introduction to MATLAB
➢MATLAB features
➢MATLAB Desktop
➢Command Window
➢History
➢Workspace
➢Current Folder

➢Scalar/Vector/Matrix
➢MATLAB basic commands

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Learning Outcomes
➢Writing script (.m files)
➢Creating user defined functions
➢Conditional statements
➢Repetitive statements
➢Plotting styles

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Script
➢Scripts or m-files are text files containing MATLAB code

➢Use the MATLAB Editor or another text editor to create a file containing the same statements
you would type at the MATLAB command line

➢Save the file under a name that ends with “.m”

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Script…

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Script…
➢A Script is a collection of MATLAB commands and functions that is bundled together in a m
file. When you run the Script, all the commands are executed sequentially.

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Script…
➢Running a m-file in the Command window

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Script Example
➢Create a script by taking a long vector and calculate of each element.

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Input & Output
➢Input:
a = input(‘Enter a number’)
➢Output:
Disp(a)

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Functions
➢Operate on information (inputs) fed into them and return outputs
➢Have a separate workspace and internal variables that is only valid inside the function.
➢MATLAB includes more than 1000 built-in functions that you can use, but sometimes you
need to create your own functions
➢Use % sign to add comments

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Functions…
➢Syntax:

Note:
➢Function name should be same as the name of script

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Functions Example

Note:
➢Name of the script should be add.m

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Conditional Statements

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Conditional Statements…
➢If A and B are scalars then it works:

➢Otherwise use:

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


MATLAB Operators

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Activity

Create a function that finds the solution for x based on different input values for a, b and
c.

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Repetitive Statements

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Activity
Create a function that finds the factorial of a number using for loop in MATLAB.

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting
➢Plotting is a very powerful feature in MATLAB
➢y=[1 3 5 3 0] 5

4.5
➢x=[0 1 2 3 4] 4

➢Plot(x,y) 3.5

2.5

1.5

0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting…
My figure
5
➢y=[1 3 5 3 0]
4.5

➢x=[0 1 2 3 4] 4

➢Plot(x,y) 3.5

3
➢Title(‘My figure’)

Amplitude
2.5

➢Xlabel(‘abc’) 2

➢Ylabel(‘Amplitude’) 1.5

0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4
abc

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting types 1

0.8

➢Plot 0.6

0.4

➢>> t=0:.01:10; 0.2

➢>> y=sin(t);
0 1

-0.2 0.9

➢>> plot(t,y) -0.4 0.8

-0.6 0.7
➢Stem -0.8 0.6

➢>>t=0:.01:2; -1
0 1 2 3 4 5 6 7 8 9 10
0.5

0.4

➢>> y=sin(t); 0.3

0.2
➢>> stem(t,y) 0.1

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting types…
5

4.5

➢Bar Graph 4

➢>> y=[1 3 5 3 0]; 3.5

➢>> x=[0 1 2 3 4]; 2.5

4
2
➢>> bar(x,y) 1.5

➢Horizontal Bar Graph


3
1

0.5

➢>> y=[1 3 5 3 0]; 0


0 1 2 3 4
2

➢>> x=[0 1 2 3 4]; 1

➢>> barh(x,y)
0

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting types…
5

➢3D Bar Graph 4

➢>> y=[1 3 5 3 0]; 2

➢>> x=[0 1 2 3 4]; 1


4
0

➢>> bar3(x,y) 0
3

1
➢Horizontal 3D Bar Graph 2
2

3
1
➢>> y=[1 3 5 3 0]; 4

➢>> x=[0 1 2 3 4];


0
➢>> bar3h(x,y) 2
4
6

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting types…
10%

➢Pie Chart
➢>>x=[1 2 3 4] 20% 40%

➢>>pie(x) A

OR
B D

➢>>x=[1 2 3 4]
30%
➢>>pie(x,{‘A’,’B’,’C’,’D’})

➢For 3D pie chart just replace the command with pie3


C

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Plotting types…
➢Pie Chart
A

➢>>x=[1 2 3 4]
➢>>Explode=[0 1 0 0] B D

➢>>pie(x, Explode, {‘A’,’B’,’C’,’D’})

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Multiple Plots in single graph
In MATLAB it is easy to plot multiple data set in one graph

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Multiple Plots in single graph..
Option 2:
Hold on
Hold off

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Multiple Plots in different graphs
Subplot
➢The subplot command enables you to display multiple plots in the same window or print
them on the same piece of paper
Figure
➢Figure command enables to open a new plot in a new window

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Subplot
5 5
subplot(2,2,1), plot(x,y), 4 4

subplot(2,2,2), stem(x,y), 3 3

2 2
subplot(2,2,3), bar(x,y), 1 1

subplot(2,2,4), barh(x,y) 0
0 1 2 3 4
0
0 1 2 3 4

4
4
3 3
2
2 1
0
1

0
0 1 2 3 4 0 2 4 6

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Figure
bar(x,y), figure, barh(x,y), figure, stem(x,y)

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk


Summary
➢Writing script (.m files)
➢Creating user defined functions
➢Conditional statements
➢Repetitive statements
➢Plotting styles

CAED Engr. Aasim Raheel asim.raheel@uettaxila.edu.pk

You might also like