Working With Data Files

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

S. V. S. S.

’s
Nanasaheb Mahadik College of Engineering,
Peth
Nanasaheb Mahadik College of Engineering,
Peth

National Level Workshop on Basics of MATLAB and it's


Engineering Applications

DEPARTMENT of
ELECTRICAL ENGINEERING

WELCOMES YOU

1
Department of Electrical
Engineering
Contents
Automating Commands with Scripts
 Importing Data
 Mixed Data Types

 Cell Arrays

 Numbers , strings and dates

 Exporting Data

2
Department of Electrical
Engineering
Command History

When you start Matlab you will see the desktop which by default is


composed of the Command Window, Command History and
Workspace.

3
Department of Electrical
Engineering
Command History

•To open the Command History window with all history


showing, in the Command Window, press the Up Arrow key (↑)
or enter command history 
•The Command History window displays a log of statements that
you ran in the current and previous MATLAB® sessions.
•The Command History lists the time and date of each session in
the short date format for your operating system, followed by the
statements from that session.

3
Department of Electrical
Engineering
Editor Window and
Command Window
 The Editor Window
 Alternatively you can write a program or script called a m-file (has a .m
extension) in the Editor Window.  The Editor Window is a word processor
specifically designed for Matlab commands so it automatically formats
certain things for you such as the command "for" below
 The Command Window
 Often times you will work at the command line in the command window.
The command window allows you to type commands directly and see the
results immediately. For example at the >>, define a variable "a" by typing
 >> a=[1 2]
 What is printed out below is the output of the command
a =
1 2

4
Department of Electrical
Engineering
Automating commands in MATLAB
scripts

3
Department of Electrical
Engineering
Creating Script Files

Steps
1.Go to Home Menu
2. select new script file
3.Open up Editor file
4.Write the program
5.Save the program and file name
6.Observe this in current folder

3
Department of Electrical
Engineering
Running script Files

Steps
1.Open editor file and write down the
program
2.Save program file
3.See this in the current folder
4.Run the program file or script
5.Observe the output in command window
6.See the output in command window

4
Department of Electrical
Engineering
Workspace Data

1.Find the values of variables in program in workspace


2.For Example a = 50;
b = 100;
Like this variable followed by semicolon that is displayed in
the workspace
3.Clear all and clc commands if we type in the command
window
4.This operation will clear command window and workspace
values also.
5.Start program with clear all and clc to remove initial
garbage values

4
Department of Electrical
Engineering
Cell in MATLAB

 A cell array is a data type with indexed data containers


called cells, where each cell can contain any type of data. Cell
arrays commonly contain either lists of character vectors of
different lengths, or mixes of strings and numbers, or numeric
arrays of different sizes.

 Structures and cell arrays are two kinds of MATLAB arrays


that can hold generic, unstructured heterogeneous data.
 A structure array is a data type that groups related data using
data containers called fields. Each field can contain any type of
data.

4
Department of Electrical
Engineering
Cell in MATLAB

4
Department of Electrical
Engineering
Cell Mode Scripts
 Cell mode is used to braking your scripts into different
sections.
 To make independent operations
make a comment with % % at the starting of each section of
script
For Example

4
Department of Electrical
Engineering
Running cell mode scripts
Steps
1.Go to editor mode cell tool bar
2.Select Evaluate cell (ctrl + Enter)
3.You can run only one section at a time
4.Go to next section and press enter
5.Press ctrl + shift +enter to next section and
then evaluate cell and run that particular section
by selecting advanced button.

4
Department of Electrical
Engineering
Publishing of Scripts

16
Department of Electrical
Engineering
Basic plot options
For example let us plot the simple function y = x for
the range of values for x from 0 to 100, with an
increment of 5.
x = [0:5:100]; OUTPUT-
y = x;
plot(x, y)

16
Department of Electrical
Engineering
Basic plot options

Adding Title, Labels, Grid Lines and Scaling on the


Graph
 MATLAB allows you to add title, labels along the x-axis and
y-axis.
 The x label and y label commands generate labels along x-axis
and y-axis.
 The title command allows you to put a title on the graph.
 The grid on command allows you to put the grid lines on the
graph.
 The axis equal command allows generating the plot with the same
scale factors and the spaces on both axes.
 The axis square command generates a square plot.

16
Department of Electrical
Engineering
Basic plot options

For example
x = [0:0.01:10];
y = sin(x);
plot(x, y),
xlabel('x'),
ylabel('Sin(x)'),
title('Sin(x) Graph'),
grid on,
axis equal

16
Department of Electrical
Engineering
Basic plot options

Drawing Multiple Functions on the Same Graph


You can draw multiple graphs on the same plot. 
For example
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g, '.-'), legend('Sin(x)', 'Cos(x)')

16
Department of Electrical
Engineering
Annotating plots

Annotations are extra information added to a chart to help


identify important information. 
 Syntax
 annotation(lineType,x,y)
 annotation(lineType)
 annotation(shapeType,dim)
 annotation(shapeType)
 annotation(___,Name,Value)
 annotation(container,___)
 an = annotation(___)

16
Department of Electrical
Engineering
Annotating plots

 annotation(lineType,x,y)
annotation(lineType,x,y) creates a line or arrow annotation
extending between two points in the current figure.
Specify lineType as 'line', 'arrow', 'doublearrow', or 'textarrow'.
Specify x and y as two-element vectors of the form [x_begin
x_end] and [y_begin y_end], respectively.
figure plot(1:10) x = [0.3 0.5]; y = [0.6 0.5];
annotation('textarrow',x,y,'String','y = x ')

16
Department of Electrical
Engineering
Annotating plots

 annotation(lineType)
annotation(lineType) creates the annotation in the default
position between the points (0.3,0.3) and (0.4,0.4).
Create a simple line plot and add a text box annotation to the figure.
Specify the text description by setting the String property. Force the
box to fit tightly around the text by setting
the FitBoxToText property to 'on'.
figure plot(1:10) dim = [.2 .5 .3 .3]; str = 'Straight Line Plot from 1
to 10'; annotation('textbox',dim,'String',str,'FitBoxToText','on');

16
Department of Electrical
Engineering
Annotating plots

 annotation(shapeType,dim)
 annotation(shapeType,dim) creates a rectangle, ellipse, or text box

annotation with a particular size and location in the current figure.


Specify shapeType as 'rectangle', 'ellipse', or 'textbox'. Specify dim as a
four-element vector of the form [x y w h]. The x and y elements determine
the position and the w and h elements determine the size.
 Create a text box annotation without setting the FitBoxToText property.
The text box uses the specified width and height and wraps text as needed.
figure plot(1:10) dim = [.2 .5 .3 .3]; str = 'Straight Line Plot from 1 to 10';
annotation('textbox',dim,'String',str)

16
Department of Electrical
Engineering
Annotating plots

 annotation(shapeType,dim)
annotation(shapeType) creates the annotation in the default
position so that the lower left corner is at (0.3,0.3) and the width
and height are both 0.1.
Create a stem plot and add a rectangle annotation to the figure.
Change the color of the rectangle outline by specifying
the Color property.
figure data = [2 4 6 7 8 7 5 2]; stem(data) dim = [.3 .68 .2 .2];
annotation('rectangle',dim,'Color','red')

16
Department of Electrical
Engineering

You might also like