ECE 381: Laboratory 1 | |
Analog Signals: Operations, periodicity, energy, and power | |
| |
|
Plotting in Matlab is easy and straightforward. Say, we have two vectors,
t and
x, holding, respectively, the sampling times and the
corresponding amplitude values for an analog signal
>> plot(t,x,'r--'); % red, dashed line >> % Trailing portion after the first percent character of each line >> % is deemed a comment, and ignored.
In the case we omitted the time vector, and entered just
We can have multiple signals plotted on the same time axis, e.g., as:
>> plot(t1,x1,'-',t2,x2,'.'); % x1: solid line, x2: dotted line
Note that t1 and t2 need not be identical time vectors; in fact, they can even be of different lengths. However, associated time and amplitude vectors, e.g., t2 and x2, must have the same length.
Alternatively, multiple signals can be plotted on the same axis by using the
hold command. Once this command is entered, the current plot, if any,
is held in the figure window, and all subsequent plots are superimposed on it.
This behavior can be cancelled by entering
Other useful plotting utilities include axis, xlabel, ylabel, title, text, gtext, grid and legend. Take the following examples as illustrations of their usage. To learn more, we can use help on these commands, or on any other Matlab command for that matter. For plotting in particular, help graph2d is useful.
>> axis([ xmin xmax ymin ymax ]); % to set the x- and y-axis ranges >> axis tight; % zoom in as much as possible without losing any part of the plot >> axis off; % no axes are drawn >> xlabel('Time t (seconds)'); >> ylabel('Amplitude x (volts)'); >> title('x_2(t) versus t'); >> text(x0,y0,'An annotation, note, etc.'); % placed at the coordinates (x0,y0) >> gtext('Another annotation'); % place it anywhere using the mouse >> grid; % draw or remove grid lines at the x- and y-axis tick marks (toggles) >> legend('plot 1','plot 2'); % draws two sample lines, and labels them as >> % 'plot 1, 'plot 2'
In the above discussion and examples, we had only one graph in mind, with one
or more plot in it. When we execute a plot command, or any of the
utility commands mentioned above, this affects either the currently active
"figure," or if there is none, one is created automatically. In Matlab, "figure"
is the window that pops up, and into which we plot data. At any point in time,
the active figure is the last figure window we had on the foreground (say, by a
mouse click on it), or the last figure window that is created. We can create
additional figure windows by entering figure at the prompt. Each figure
window thus created has an integer handle, automatically assigned by Matlab.
This information is available on the title bar of the window, e.g.,
>> figure(5); % create new figure window with handle 5
This also provides for bringing a figure window to the foreground, and thus, making it the currently active figure:
>> figure(5); % create new figure window with handle 5 >> figure(11); % create yet another one, which becomes active >> figure(5); % make "Figure No. 5" active again
We can export the contents of the active figure window in different formats. This is exemplified for the two most common formats:
>> print('-dps','filename.ps'); % black-and-white postscript >> print('-djpeg','filename.jpeg'); % standard quality JPEG image
The files filename.* are created in the present working directory, and they are to reflect what we see in the active figure window on the computer screen. It is not a bad idea to view the outcome by an appropriate tool (gsview, a browser, an image viewer, etc.) because the quality may sometimes be low. Of course, help print tells about many other formats and configuration options. Also check out the File menu options in the figure window. We can export the contents via the Export option under this menu, or send the contents directly to a printer.
Finally, as a means of organizing multiple plots in one figure window, we can use subplots. This is like opening a clean slate into which we can plot, but no new figure window is created. Instead, the active figure window is divided into a two-dimensional array of separate plotting areas, or boxes, each with its own title, x- and y-axis labels, axis range settings, annotations, etc. Take the following example:
>> figure(2); clf; % make "Figure No. 2" active; and, clear its contents >> subplot(2,3,1); % 2-by-3 array of plotting areas >> % the first box, box(1,1), is activated >> plot(t1,x1); % plotted in box(1,1) >> subplot(2,3,3); % box(1,3) is active (row 1, column 3) >> plot(t3,x3); % now box(1,3) is affected >> hold on; >> plot(t30,x30); % have a second plot in box(1,3) >> subplot(2,3,2); >> title('Nothing plotted yet'); % this is the second box, box(1,2)So, the plotting areas are indexed in row major order, and the second row in the 2-by-3 layout of this example has areas 4, 5, and 6 from left to right. But, the example could continue as follows:
>> subplot(2,1,2); % layout switched to 2-by-1 >> % second row, or box(2,1), is active >> plot(t4,x4); % spans the entire 2nd row >> % existing subplots of the 1st row unaffected
Obviously, the presentation quality of Matlab figures can be enhanced in a lot of ways. One needs to try and experiment with ideas and available tools. For the simple plots that we have in this assignment, above should be sufficient.
As for Matlab scripts, called m-files, these are plain text files with the extension ".m", e.g., "myscript.m". An m-file simply contains Matlab statements and commands in the same way as one would enter them at the prompt. All command-prompt rules apply. If a statement is terminated by a semi-colon, the result is not echoed; otherwise, it is echoed. If a statement is too long to fit on one line, escape to the next line is possible by using an ellipsis "...". Control structures such as if-elseif-else-end, or for-end can be used. So on, so forth. For example, all statements in the above examples could be put in an m-file, say examp.m, and executed by entering examp at the command prompt. Important point to note about Matlab scripts is that they execute in the global scope. So, all variables defined in a Matlab script are available after the completion of its execution. If such a variable happens to already exist in the global scope, it will be overwritten by the script's execution. For example, if examp.m contained nothing but the example statements of this overview, then the various time and amplitude vectors used would have to be defined in the global scope prior to executing examp. Otherwise we would get errors. Scoped execution is possible through the use of Matlab functions, which are again m-files with a special header, but functions are not needed in this assignment. Still, Exercise 4 could be done nicely by writing a function, which can be reused for similar problems with different parameters, or even with different number of terms in the combination.
School of Computing and Engineering University of Missouri - Kansas City |
Last updated: January 21, 2006 |