L02 - MATLAB2 Fprintf Fopen PDF
L02 - MATLAB2 Fprintf Fopen PDF
Lecture 2 - MATLAB 2
Mongkol JIRAVACHARADET
Input/Output Commands
Command Description
disp (A) Displays the contents, but not the name, of the
Array A.
disp (‘text’) Displays the text string enclosed within single quotes.
x = input(‘text’) Displays the text in quotes, waits for user input from
the keyboard, and stores the value in x.
x = input(‘text’,’s’) Displays the text in quotes, waits for user input from
the keyboard, and stores the input as a string in x.
INPUT AND OUTPUT
function s = inputAbuse
% inputAbuse Use input messages to compute sum of 3 variables
Text Output
Since disp requires only one argument, the message and variable
must be combined into a single string.
Syntax Description
fprintf(‘format’,A,...) Displays the elements of the array A, and any
addition array arguments, according to the format
specified in the string ‘format’
‘format’ structure %[-][number1.number2]C, where
number1 specifies the minimum field width,
number2 specifies the number of digits to the
right of the decimal point, and C contains control
codes and format codes. Items in brackets are
optional. [-] specifies left justified.
fprintf(format)
fprintf(format,variables)
fprintf(fid,format,variables)
fprintf (Continue…)
\\ Backslash.
fprintf(format,variables)
>>r = [2.25:20:42.25];
2.25 14.137
>>circum = 2*pi*r;
22.25 139.8
>>y = [r;circum];
42.25 265.46
>>fprintf(‘%5.2f %11.5g\n’,y)
>> x = ...
>> fout = fopen(‘myfile.dat’,’wt’);
>> fprintf(fout,’ k x(k)\n’);
>> for k = 1:length(x)
>> fprintf(fout,’%4d %5.2f\n’,k,x(k));
>> end
>> fclose(fout)
Creating a Plot
The plot function has different forms, depending on the input arguments.
If y is a vector, plot(y) produces a piecewise linear graph of the elements of y
versus the index of the elements of y.
If you specify two vectors as arguments, plot(x,y) produces a graph of y versus
x.
For example, these statements use the colon operator to create a vector of x values
ranging from zero to 2π, compute the sine of these values, and plot the result.
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
Now label the axes and add a title. The characters \pi create the symbol π.
Multiple x-y pair arguments create multiple graphs with a single call to plot.
MATLAB automatically cycles through a predefined (but user settable) list of colors to
allow discrimination among sets of data.
For example, these statements plot three related functions of x, each curve in a
separate distinguishing color.
>> y2 = sin(x-.25);
>> y3 = sin(x-.5);
>> plot(x,y,x,y2,x,y3)
>> legend('sin(x)','sin(x-.25)','sin(x-.5)')
It is possible to specify color, line styles, and markers (such as plus signs or
circles) when you plot your data using the plot command.
>> plot(x,y,'color_style_marker')
color_style_marker is a string containing from one to four characters
(enclosed in single quotation marks) constructed from a color, a line style, and
a marker type:
• Color strings are 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'.
These correspond to cyan, magenta, yellow, red, green, blue, white, and black.
• Linestyle strings are '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot.
Omit the linestyle for no line.
• Marker types are '+', 'o', '*', and 'x' and the filled marker types are :
's' for square, 'd' for diamond, '^' for up triangle, 'v' for down triangle,
'>' for right triangle, '<' for left triangle, 'p' for pentagram, 'h' for hexagram,
and none for no marker.
>> x1 = 0:pi/100:2*pi;
>> x2 = 0:pi/10:2*pi;
>> plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
Adding Plots to an Existing Graph
The hold command enables you to add plots to an existing graph. When you type
hold on
MATLAB does not replace the existing graph when you issue another plotting
command; it adds the new data to the current graph, rescaling the axes if
necessary.
For example, these statements first create a contour plot of the peaks function, then
superimpose a pseudocolor plot of the same function.
[x,y,z] = peaks;
contour(x,y,z,20,'k')
hold on
pcolor(x,y,z)
shading interp
hold off
The hold on command causes the pcolor
plot to be combined with the contour
plot in one figure.