Академический Документы
Профессиональный Документы
Культура Документы
Lecture 2 - MATLAB 2
- Save & Load - Input & Output - Plotting Graph
Mongkol JIRAVACHARADET
>> whos >> load trigvar >> whos Loading Data from Plain Text Files >> clear >> whos >> XY = load(xyvals.txt) >> x = XY(:,1) >> y = XY(:,2)
Input/Output Commands
Input User
Command disp (A) disp (text) fprintf x = input(text)
Output
- Screen - File
MATLAB
Description Displays the contents, but not the name, of the Array A. Displays the text string enclosed within single quotes. Control the screens output display format. 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.
Text Output
The disp Function
>> disp(My favorite color is red) >> Speed = 63; >> disp(The vehicles predicted speed is:) >> disp(Speed) Since disp requires only one argument, the message and variable must be combined into a single string. >> yourName = input(enter your name ,s); >> disp([Your name is ,yourName])
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 (Continue)
Format Codes Description Scientific format with lowercase e. Scientific format with uppercase E. Decimal format.
%e %E %f %g
Control Codes
\n \r \b \t \\
fprintf(format,variables)
>> name = Elvis; age = str2num(datestr(now,10)-1935; >> fprintf(%s is %d years old\n,name,age); Elvis is 65 years old >> fprintf(The speed is: %3.1f\n,Speed) The speed is: 63.2
>>r = [2.25:20:42.25]; 2.25 >>circum = 2*pi*r; 22.25 >>y = [r;circum]; 42.25 >>fprintf(%5.2f %11.5g\n,y) 265.46 139.8 14.137
Permission:
r rt w wt a r+ w+ a+ W A read read plain text file write write plain text file append read & write truncate or create for read & write read & append write without automatic flushing append without automatic flushing
fprintf(fid,format,variables)
>> x = ... >> fout = fopen(myfile.dat,wt); >> fprintf(fout, k x(k)\n); %5.2f\n,k,x(k)); >> for k = 1:length(x) >> fprintf(fout,%4d >> 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.
The legend command provides an easy way to identify the individual plots.
>> legend('sin(x)','sin(x-.25)','sin(x-.5)')
>> 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.
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.