Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Computational Mathematics Lab

Data Plotting (2D), Saving to File, and Formatted Printing in Command Window

R Prasanth Kumar

Department of Mechanical and Aerospace Engineering


Indian Institute of Technology Hyderabad
2015

2D Plots
Basics


Type the following in the MATLAB command window. Do not close the gure.

>> x1 = 0:0.1:10;y1 = x1.^2;


>> plot(x1,y1)


Default color for plots with single set of data points is blue solid line of width 0.5

For multiple plots in the same gure window:

>> x2 = 0:0.1:10;y2 = x2.^2.5;


>> x3 = 0:0.1:10;y3 = x3;
>> plot(x1,y1,x2,y2,x3,y3)


Type the following for labels, title, and legend.

>> xlabel(x data);ylabel(y data);title(Title of the figure)


>> legend(First,Second,Third)


Legend can be moved by clicking and dragging using left mouse key. You can right
click on the legend and select appropriate location and orientation.

Close the gure window.

2D Plots
Drawing a Semi-Circle


Create data points for a semicircle of radius 1 unit and use plot command

>> x = -1:0.01:1;y = sqrt(1-x.^2);plot(x,y)


Looks like a semi-ellipse rather than semi-circle due to unequal scaling along x and
y axes.


For equal scaling in x and y axes:

>> axis equal


You may nd that axis limits have changed.


For setting axis limits for x and y, use axis([xmin xmax ymin ymax]) as follows:

>> axis([-1 1 0 1])




Display grid in the current plot.

>> grid on


Close the gure window.

2D Plots
Line Colors, Width, and Type


Specify line colors

>> x1 = 0:0.1:10;x2 = x1;y1 = x1.^2;y2 = x2;


>> plot(x1,y1,k,x2,y2,r);


k and r are line color speciers.


Specify line styles (dotted and dashed) along with color speciers

>> plot(x1,y1,k:,x2,y2,r--);


Specify line width for all the data sets in the current gure.

>> plot(x1,y1,k:,x2,y2,r--,LineWidth,2);


Note that, it is not possible to specify LineWidth for each curve separately using plot
function. LineWidth property should be used in plot function at the end or after all
the data sets have been specied. You can always change the line properties by
selecting edit plot (arrow button) from gure window and right clicking on the
individual plots.

For more information on plot function

>> help plot

2D Plots
Plotting in Multiple Figure Windows


If there is already a gure window open, plot function draws in the current gure
window rather than opening a new gure.

For plotting in a new gure window:

>>
>>
>>
>>
>>
>>

x1 = 0:0.1:10;y1 = x1.^2;x2 = x1;y2 = x2.^2.2;x3 = x1;y3 = x3;


plot(x1,y1)
figure
plot(x2,y2)
figure
plot(x3,y3)

The gure command creates a new gure window and makes it active so that any
plot command that is used after it will display into the new gure window.


Close all gure windows at once:

>> close all

2D Plots
Subplots in a Figure Window


Plots can be created in tiled positions in a gure window using subplot function.

To make plots three rows and one column:

>>
>>
>>
>>

x1 = 0:0.1:10;y1 = x1.^2;x2 = x1;y2 = x2.^2.2;x3 = x1;y3 = x3;


subplot(3,1,1), plot(x1,y1), xlabel(x1), ylabel(y1)
subplot(3,1,2), plot(x2,y2), xlabel(x2), ylabel(y2)
subplot(3,1,3), plot(x3,y3), xlabel(x3), ylabel(y3)

To make plots in two rows and two columns:

>>
>>
>>
>>

subplot(2,2,1),
subplot(2,2,2),
subplot(2,2,3),
subplot(2,2,4),

plot(x1,y1),
plot(x2,y2),
plot(x3,y3),
plot(x1,y1),

xlabel(x1),
xlabel(x2),
xlabel(x3),
xlabel(x4),

ylabel(y1)
ylabel(y2)
ylabel(y3)
ylabel(y4)

Save Data to File and Retrieve Data from a File




You may want to save the result data produced after a computation. To save all
variables in workspace, use save <lename.mat> as follows:

>> save alldatavars.mat


where alldatavars is the le name.


To save only some variables (lets say a, b, and c variables) to a le with name data

>> save(data.mat,a,b,c)
Note that, save function saves the le in the current folder by default.


To load an existing data le

>> load data.mat


>> load(data.mat,a)
The latter command loads only variable a from data.mat.

Formatted Printing in Command Window




When a variable name at the end of a line is not followed by semicolon, it is printed
in the command window. Each variable takes up to ve lines.

>> x = 10.12345678;
>> x


In order to save space, we can use disp() function

>> disp(x)
Disp function consumes two lines and does not allow formatting.


For formatted printing, fprintf() function can be used with formatting speciers as
in C language.

>> fprintf(x = %6.3f \n,x);


Format speciers start with % sign. \n stands for new line. In the above example, 6
is the total number of digits including dot, 3 is number of digits after dot.

>> x = [1.23456 -4.555];


>> fprintf(%7.2f %7.3f\n,x)

You might also like