MATLAB Programming Lecture 2
MATLAB Programming Lecture 2
Eng./ Essamand
Nabil
Drives
Lecture No.“2”
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
The Language of Technical
Curve fitting Computing
Symbolic math.
Signal processing
Prepared by:
Function m-file
Eng./ Essam Nabil
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
MATLAB Plotting
SIMATIC HMI
MATLAB
The Human
Programming
figure : is a MATLAB window that contains graphic displays.
Machine Interface
Introduction
plot :is any graphic display you can create within a figure.
Figures can contain any number of plots. Each plot is
MATLAB Basics
created within a 2-D or a 3-D data space called an axes.
Vectors & Arrays
Math. Functions
graph :is a plot of data within a 2-D or 3-D axes.
Polynomials
MATLAB plotting
To illustrate the basic components of a graph, execute the
2-D & 3-D Graphics following
Flow Control plot of a family of sine curves:
System analysis
MATLAB Plotting
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Figure Toolbars
SIMATIC HMI
MATLAB Figure Toolbar : provide shortcuts to access commonly used features.
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
Camera Toolbar :Use for manipulating 3-D views.
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Figure Toolbars
SIMATIC HMI
MATLAB
The Human
Plot Edit Toolbar — Use for annotation and setting object properties.
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
Note: Display the selected toolbar from the View menu
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
>>axis equal :sets the aspect ratio so that the data units are the
same in every direction. The aspect ratio of the x-,
Polynomials y-, and z-axis is adjusted automatically according to
MATLAB plotting the range of data units in the x, y, and z directions.
2-D & 3-D Graphics >>axis image :is the same as axis equal except that the plot box
Flow Control
fits tightly around the data.
System analysis
>>axis square :makes the current axes region square (or cubed
when three-dimensional). This option adjusts the x
Curve fitting
axis, y-axis, and z-axis so that they have equal
Symbolic math. lengths and adjusts the increments between data
Signal processing units accordingly.
Function m-file >>axis off :turns off all axis lines, tick marks, and labels.
MATLAB Simulink >>axis on :turns on all axis lines, tick marks, and labels.
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
>>box off :does not display the boundary of the current axes.
Function m-file
>>box :toggles the visible state of the current axes boundary.
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
>>grid off : removes grid lines from the current axes.
MATLAB Simulink
>>grid : toggles the grid lines visibility state.
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
1. Line Graphs
Programming
Machine Interface
Introduction
MATLAB Basics
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
plot(X,Y) :plots the values of Y versus the values of X.
Machine Interface
Introduction
MATLAB Basics t=0:0.1:1;
Vectors & Arrays y=5*exp(-2*t);
Math. Functions plot(t,y)
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
t=0:0.05:10;
Symbolic math. y=sin(t);
Signal processing plot(t,y)
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
plot(X1,Y1, X2,Y2, X3,Y3,…) :plots all lines defined by
Machine Interface
Introduction
Yn versus Xn pairs.
MATLAB Basics
Math. Functions
Polynomials t=0:0.05:10;
MATLAB plotting y1=sin(t);
2-D & 3-D Graphics y2=cos(t);
Flow Control plot(t,y1,t,y2)
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
to specify the properties of lines used for plotting use:
Machine Interface
Introduction
plot (X, Y,‘color symbol line style’ ,'linewidth', 2)
MATLAB Basics
t=linspace(-3*pi,3*pi,100);
Vectors & Arrays
y=sin(t)./t;
Math. Functions
Polynomials
plot( t, y,‘ r*:‘ ,‘ linewidth‘, 2)
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Color, Symbols, and Line Types:
Programming
Machine Interface
Introduction
Colors Symbols Line Types
MATLAB Basics b blue . point - solid
Vectors & Arrays g green o circle : dotted
Math. Functions r red x x-mark -. dash dot
Polynomials c cyan + plus -- dashed
MATLAB plotting
m magenta * star
y yellow s square
2-D & 3-D Graphics
k black d diamond
Flow Control
v triangle (down)
System analysis
^ triangle (up)
Curve fitting < triangle (left)
Symbolic math. > triangle (right)
Signal processing p pentagram
Function m-file h hexagram
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
>>xlabel('string') :labels the x-axis of the current axes.
Programming
Machine Interface >>ylabel('string') :labels the y-axis of the current axes.
Introduction
>>zlabel('string') :labels the z-axis of the current axes.
MATLAB Basics
MATLAB plotting
t=0:0.05:10;
2-D & 3-D Graphics
y1=sin(t);
Flow Control
y2=cos(t);
System analysis
plot(t,y1,'b*-',t,y2,'r.:')
Curve fitting
xlabel('Time (s)')
Symbolic math.
Signal processing
ylabel('Amplitude (v)')
Function m-file
title('Sinusoidal signals')
MATLAB Simulink
legend('sin' , 'cos')
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
>>figure :creates a new figure object .
Programming
Machine Interface >>hold on :retains the current plot and axis properties.
Introduction
>>hold off :resets the current plot and axis properties.
MATLAB Basics
MATLAB plotting
t=0:0.05:10;
2-D & 3-D Graphics
y1=sin(t);
Flow Control
y2=cos(t);
System analysis
y3=tan(t);
Curve fitting
Symbolic math.
plot(t,y1), hold on
Signal processing plot(t,y2)
Function m-file figure
MATLAB Simulink plot(t,y3)
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
text - Create text object in current axes.
The Human
Programming
Machine Interface >>text(x,y,'string') :adds the string in quotes to the location
Introduction specified by the point (x,y) x and y must be
MATLAB Basics numbers of class double.
Vectors & Arrays >>text(x,y,z,'string') :adds the string in 3-D coordinates. x, y
Math. Functions and z must be numbers of class double.
Polynomials >>gtext('string') :displays a text string in the current figure
MATLAB plotting window after you select a location with the
2-D & 3-D Graphics
mouse.
Flow Control
System analysis
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
>>[x,y] = ginput(n) : enables you to select n points from the
Programming
Machine Interface current axes and returns the x- and y
Introduction coordinates in the column vectors x and
MATLAB Basics y, respectively.
Vectors & Arrays Note:
Math. Functions [x,y] = ginput gathers an unlimited number of points .
Polynomials
MATLAB plotting
t=0:pi/20:2*pi;
2-D & 3-D Graphics
plot(t,sinc(t))
Flow Control
text(pi,0,' sinc(\pi)')
System analysis
ginput(2)
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Adding special characters such as Greek letters and mathematical symbols
Programming by using the backslash character (\) before the character sequence name:
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI clear all, close all, clc
MATLAB
The Human
Programming theta=linspace(0,2*pi);
Machine Interface
y=sinc(theta);
Introduction
plot(theta,y),grid on
MATLAB Basics
xlabel('\theta')
Vectors & Arrays
ylabel('\gamma')
Math. Functions
gtext('\fontname{courier}\fontsize{16}\bf X_{\alpha}
Polynomials
+Y^{2\pi}')
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
stairs(Y) :draws a stairstep graph of the elements of
Vectors & Arrays
the vector Y
Math. Functions
Polynomials
stairs(X,Y) :plots a stair step graph of the elements in Y
MATLAB plotting
at the locations specified in X.
2-D & 3-D Graphics
Flow Control
System analysis
x=0:0.25:10;
Curve fitting
y=sin(x);
Symbolic math.
stairs(x,y)
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Eng./ Essamand
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering