%%
%
%bx
''
'
Beneath is a script named “nm113.m”, which uses the command ‘ input
’ so that the user could input some data via the keyboard. If we run the script, it gets a value of the temperature in Fahrenheit (F) via the keyboard from the user, converts it into the temperature in Centigrade (°C) and then prints the results with some remarks both onto the screen and into a data file named ‘nm113.txt’.
%nm113.m f=input('Input the temperature in Fahrenheit[F]:'); c=5/9*(f-32); fprintf('%5.2f(in Fahrenheit) is %5.2f(in Centigrade).\n',f,c) fid=fopen('nm113.txt','w') fprintf(fid,'%5.2f(Fahrenheit) is %5.2f(Centigrade).\n',f,c) fclose(fid)
In case you want the keyboard input to be recognized as a string, you should add the character 's'
as the second input argument.
>ans=input('Answer <yes> or <no>: ','s')
1.1.4 Two‐Dimensional (2D) Graphic Input/Output
How do we plot the value(s) of a vector or an array? Suppose data reflecting the highest/lowest temperatures for five days are stored as a 5 × 2 array in an ASCII file named ‘temp.txt’.
The job of the MATLAB script “nm01f01.m” is to plot these data. Running the script yields the graph shown in Figure 1.1a. Note that the first two lines are comments about the name and the functional objective of the program (file), and the fifth and sixth lines are auxiliary statements that designate the graph title and units of the vertical/horizontal axis; only the third and fourth lines are indispensable in drawing the colored graph. We need only a few MATLAB statements for this artwork, which shows the power of MATLAB.
Figure 1.1 Plot of a 5 × 2 matrix data representing the variations of the highest/lowest temperature. Domain of the horizontal variable unspecified (a) and specified (b).
%nm01f01.m % plot the data of a 5x2 array stored in "temp.txt" load temp.txt % load the data file "temp.txt" clf, plot(temp) % clear any existent figure and plot title('The highest/lowest temperature of these days') ylabel('degrees[C]'), xlabel('day')
Here are several things to keep in mind.
The command ‘ plot()’ reads along the columns of the 5 × 2 array data given as its input argument and recognizes each column as the value of a vector.
MATLAB assumes the domain of the horizontal variable to be [1 2 … 5] by default, where 5 equals the length of the vector to be plotted (see Figure 1.1a).
The graph is constructed by connecting the data points with the straight lines and is piecewise‐linear (PWL), while it looks like a curve as the data points are densely collected. Note that the graph can be plotted as points in various forms according to the optional input argument described in Table 1.2.
1 (Q1) Suppose the data in the array named ‘temp’ are the highest/lowest temperatures measured on the 11th, 12th, 14th, 16th, and 17th days, respectively. How should we modify the above script to have the actual days shown on the horizontal axis?
2 (A1) Just make the day vector [11 12 14 16 17] and use it as the first input argument of the ‘ plot()’ command.>days=[11 12 14 16 17]; plot(days,temp)Running these statements yields the graph in Figure 1.1b.
3 (Q2) How do we change the ranges of the horizontal/vertical axes into 10–20 and 0–30, respectively, and draw the grid on the graph?
4 (A2)>axis([10 20 0 30]), grid on
5 (Q3) How can we change the range of just the horizontal or vertical axis, separately?
6 (A3)>xlim([11 17]); ylim([0 30])
7 (Q4) How do we make the scales of the horizontal/vertical axes equal so that a circle appears round, not like an ellipse?
8 (A4)>axis('equal')
9 (Q5) How do we fix the tick values and their labels of the horizontal/vertical axes?
10 (A5)>set(gca,'xtick',[11:3:17],'xticklabel',{'11','14','17'}) >set(gca,'ytick',[5:5:15],'yticklabel',{'5','15','25'})where gca is the current (figure) axis and gcf is the current figure handle.
11 (Q6) How do we have another graph overlapped onto an existing graph?
12 (A6) If you run the ‘ hold on’ command after plotting the first graph, any following graphs in the same section will be overlapped onto the existing one(s) rather than plotted newly. For example:>hold on, plot(days,temp(:,1),'b*', days,temp(:,2),'ro') This will be good until you issue the command ‘ hold off’ or clear all the graphs in the graphic window by using the ‘ clf’ command.
Table 1.2 Graphic line specifications used in the ‘ plot()
’ command.
Line type | Point type (marker symbol) | Color | |||||||||
‐
|
Solid line | · | Dot | + | Plus | * | Asterisk |
r :
|
Red |
m :
|
Magenta |
:
|
Dotted line |
̂ :
|
Δ |
> :
|
> |
o
|
Circle |
g :
|
Green |
y :
|
Yellow |
‐‐
|
Dashed line |
p :
|
* |
v :
|
∇ |
x :
|
x‐Mark |
b :
|
Blue |
c :
|
Cyan (sky blue) |
‐.
|
Dash‐dot |
d :
|
⋄ |
< :
|
|