Professional Documents
Culture Documents
Computational Mathematics Lab: 2D Plots
Computational Mathematics Lab: 2D Plots
Data Plotting (2D), Saving to File, and Formatted Printing in Command Window
R Prasanth Kumar
2D Plots
Basics
Type the following in the MATLAB command window. Do not close the gure.
Default color for plots with single set of data points is blue solid line of width 0.5
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.
2D Plots
Drawing a Semi-Circle
Create data points for a semicircle of radius 1 unit and use plot command
For setting axis limits for x and y, use axis([xmin xmax ymin ymax]) as follows:
>> grid on
2D Plots
Line Colors, Width, and Type
>> 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.
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.
>>
>>
>>
>>
>>
>>
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.
2D Plots
Subplots in a Figure Window
Plots can be created in tiled positions in a gure window using subplot function.
>>
>>
>>
>>
>>
>>
>>
>>
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)
You may want to save the result data produced after a computation. To save all
variables in workspace, use save <lename.mat> as follows:
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.
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
>> 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.