Programming examples

 

 

 

The following 22 examples are included in the plt package (in the demo folder) to help you learn how to take advantage of many of plt's features.

Also included in the demo folder is demoplt.m, a script which makes it easy to start any of the example programs by clicking on the appropriate button or to run all of them in sequence (in the order listed below) by clicking the "All Demos" button. Before each program is started, the help text for the program appears in the listbox below the 3 rows of buttons. Running demoplt is a good way to verify that plt is installed properly and gives you a quick overview of plt's capabilities. Just type cd plt\demo  and then demoplt at the command prompt. (The cd command is not necessary if you add the demo folder to the MatLab path).



plt5.m

This is a simple script which creates a plot containing 5 traces. Hopefully you have already been running this script while following thru the earlier sections.

  • Note how the five y-vectors are combined to form a single plt argument.
  • Note the use of the 'Xlim' and 'Ylim' arguments to control the initial axis limits.
  • Note the use of 'FigName'to name the figure that contains the plot
  • Note the use of  'LabelX' argument to assign a label for the x axis.
  • Note the use of the 'LabelY' argument to add a label for both the left and right-hand axes.

Note that plt will use a right-hand axis since two labels were included in the LabelY parameter. Usually the 'Right' parameter is included to specify which traces are to use the right axis, but in this example the parameter was omitted, so plt defaults to putting just the last trace on the right-hand axis.
 

plt50.m

This script is similar to plt5.m except the number of functions plotted has been increased to 50 to demonstrate the use of the TIDcolumn parameter.
  • Normally the legend is rendered in a single column; but with this many traces, the trace IDs fit much more comfortably in two columns. The 'TIDcolumn',25 included in the plt argument list actually specifies the number of items to put in the second column, which in this example means that both columns will contain 25 items.
  • With so many traces, the utility of using the legend to selectively enable or disable individual traces becomes even more compelling. Although the traces and the legend are color coded, it's difficult to distinguish every trace based on color, so clicking on a legend item is often necessary to uniquely identify a trace. This also is essential when you need to reduce the clutter that often results from plotting so many traces.
  • The 'Position' parameter is used to increase the figure area about 30% from the default of 700x525 pixels to 800x600 to make the plot less crowded.

pltn.m

Similar to plt5 and plt50, except that this is a function instead of a script. This function takes an argument which specifies how many traces to plot. For instance pltn(1) will plot a single trace pltn(500) will plot a truly incomprehensible 500 traces! Some things to note about pltn are:
  • If you don't specify an argument, 99 traces are plotted (the most you can do with the automatic legend capability).
  • Every 9th trace is put on the right hand axis.
  • The TIDcolumn parameter is used to divide the trace IDs into up to three columns if necessary. (Showing 99 trace IDs in one column wouldn't be practical.)
  • TraceIDs are disabled when more than 99 traces are specified. (Otherwise plt would complain.)
  • Uses the 'Ystring' parameter to show a continuous readout of the cursor index.
  • Uses the 'Xstring' parameter to show a continuous readout of the date and time corresponding to the cursor X position. Note the edit box form is selected by placing a question mark character at the beginning of the string.
  • A callback is written for the Xstring edit box that moves the cursor to the index with a corresponding time as close as possible to the entered value. For example, try this:
    1) Click on the top trace (which makes it easy to see the cursor).
    2) Enter dates into the edit box - e.g. "1-jan", "3-jan-07 9:59", etc.
    3) Verify that the cursor moves to the corresponding point
  • Uses the 'Options' argument to enable the x-axis cursor slider (which appears just below the peak and valley finder buttons).

pltvbar.m

This script demonstrates the use of vbar.m and ebar.m to plot vertical bars and error bars respectively. Some things to note about pltvbar are:
  • The first vbar in the argument list plots two functions on a single trace (green) with the 1st function (phase1) defining the position of the bottom of the vertical bars and the 2nd function (phase2) defining the position of the tops of the bars.
  • The second vbar in the list plots 3 functions (called serp, bell1, and bell2). The 3 columns of the first vbar argument define the x coordinates for those three functions. The next argument (0) indicates that the bottom of all the vertical bars is at y=0. The last vbar argument gives the y coordinate for each of the 3 functions (one function per column).
  • The next trace definition (the data argument pair after the 'linewidth') plots two traces corresponding to the two columns of poly23. The 1st column is a 2nd order polynomial and the 2nd column is 3rd order.
  • The next trace definition uses ebar function to create two error bar traces, the first trace defined by the first column of each of the 3 arguments and the second trace defined by the second column.
  • The 'screensize' property is used to make the plot fill a fixed fraction of the space available.
  • The 'linewidth' argument appears in the middle of the plt call to change the width of only the traces defined earlier in the argument list.
  • The 'TraceID' argument is used to assign names for each trace that are appropriate for the data being displayed.
  • The 'AxisPos' argument is used to widen the TraceID box to make room for the longer than usual trace ID names
  • The 'Options' argument is used to enable the menu bar and to add the "Print" tag to the menu box.
  • The '+FontSize','+FontWeight','+FontAngle','++Xtick','+Ytick' arguments are used to modify the main axis properties of the same name (without the plus).

pltquiv.m

This script file demonstrates several plt tricks and features:
  • The quiv.m function appears twice in the plt argument list to plot two vector fields both with the base location specified by f and lengths specified by v1 and v2 respectively. The first of these quiv calls is somewhat similar to the  MatLab command quiver(real(f),imag(f),real(v1),imag(v1));
  • Using tex commands (e.g. \uparrow) inside Trace ID names
  • Reassigning menu box items. In this example, the LinX tag is replaced by a Filter tag. Its buttondown function (which is executed when you click on 'Filter') searches for the 4th trace (using findobj) and swaps the contents of its user data and y-axis data.
  • Adding text items to the figure. Note that the text position is specified using x and y axes coordinates.
  • Using NaNs (not a number) to blank out portions of a trace. In this case, the NaNs were inserted into the x coordinate data, although using the y or z coordinates for this purpose works equally as well.
  • Using the TraceID callback function (TIDcback) to perform an action when you click on a trace ID. For example, when you click on the last trace ID (humps+rand) this will appear in the command window: "A trace named humps+rand and color [1.00 0.00 0.00] was toggled". (This TraceID callback was contrived to use all the substitutions, and is not particularly useful.)

gauss.m

This script plots the results of combining uniform random variables.

  • Shows the advantage of setting the line data after the plt(..) call.
  • Note the use of the 'FigName' and 'TraceID' arguments.
  • Note the appearance of the Greek letter in the x-axis label.
  • Shows how to use the 'COLORc' argument to select Matlab's default plotting colors (typically set to use a white background for the plotting area)
  • Shows how to use the 'DIStrace' argument to disable some traces on startup.

tasplt.m

This script file creates two plots each consisting of 9 traces. These plt tricks and features are demonstrated:
  • Note that these figures plot multiple valued functions (i.e. relations).
  • The first plot (efficiency and range chart) creates a trace for each column of gph and mpg (9 columns for 9 altitudes)
  • Demonstrates adding an additional axis to show alternate units on the right hand and/or top axis
  • Demonstrates adding text objects to annotate the graph
  • Demonstrates how the cursors in two plots can be linked. Moving one, moves the other. Also in this example switching the active trace in one plot does the same in the other as well.
  • Uses 'Xstrings' and 'Ystrings' to display alternate units.
  • Shows how to close both figures when either plot is closed.

trigplt.m

This example demonstrates the use of the slider pseudo object as well as showing how to use the TraceMK parameter to show the line characteristics in the trace selection box.

Text objects are used to display help information at the top of the plot window. This help text disappears when any parameter is changed but can be re-enabled by clicking on the help button.
 

subplt.m

 The 'SubPlot' argument is used to create 3 axes. plt puts a single trace on each axes except for the main (lower) axis which gets all the remaining traces. In this case, since there are 5 traces defined, the main axis has 3 traces. Note that the traces are assigned to the axes from the bottom up so that the last trace (serp) appears on the upper most axis.
  • The 'LabelY' argument defines the y-axis labels for all three axes, again from the bottom up. You can also define the y-axis label for the right hand main axis, by tacking it onto the end of the LabelY array (as done here).
  • The 'Right',2 argument is used to specify that the 2nd trace of the main axis should be put on the right hand axis. If this argument was omitted, plt would still have known that a right hand axis was desired (because of the extra y-label in the LabelY array) however it would have put trace 3 on the right hand axis. (By default, the last trace goes on the right axis).
  • The LineWidth and LineStyle arguments define line characteristics for all 5 traces.
  • The TraceMK parameter enables the trace selection box to show the line characteristics and the AxisPos parameter widens the trace selection box to make room for this.
  • Note that the lower (main) axis has all the usual plt cursoring features. The other 2 plots support a subset of the cursoring features and have individual color coded y-value cursor readouts shown along the bottom portion of the figure.
  • Only a single x-axis edit box is needed since plt keeps the cursors of all three axes aligned. Also note that if you zoom or pan any of the 3 plots, the other two plots will adjust their x-axis limits to agree.

subplt8.m

This script shows a slight expansion of the ideas found in subplt.m by increasing the number of axes from 3 to 8. Also the axes are arranged in two columns which allows the use of two different x axes (one for each column).
  • Note that the four axes on the left are synchronized with each other as well as the four on the right, although the left and right halves are independent of each other and have different x axis limits and units.
  • The main plot (lower left) contains 3 traces because 10 traces have been defined and 7 of them are assigned to the other 7 subplots. The main plot doesn't have a right hand axis in this example, although it would if a 'Right' argument was included or if an extra y-label were included in the 'LabelY' array.

winplt.m

This function displays traces showing the time and frequency domain shapes of 31 different FFT windows. (The last 2 are user defined.)
  • Demonstrates how to provide application specific help from a menu box tag (HelpW  in this example).
  • The right hand axis is used to good advantage.
  • Demonstrates one way to add auxiliary controls to a figure created by plt.
  • The controls added include three types of pseudo objects, as well as the native Matlab text objects and uicontrols (the checkbox in this case).
  • The pseudo objects were created using the plt('slider'), the plt('pop'), and the plt('edit') commands. All three of those commands are described here.
  • Shows how the popup object can be used to edit a vector - the window kernel in this case. (See ID30 - adjust kernel)
  • For a more complete description of this example click on the HelpW tag in the menu box or open winplt.chm or winplt.pdf.

editz.m

This function demonstrates the usefulness of plt's data editing capability.
  • Two plots are created, one showing the poles and zeros of a z-plane transfer function and the other showing the magnitude and phase of it's frequency response.
  • The frequency response plot automatically updates every time you move any of the z-plane roots using the mouse or direct keyboard entry.
  • In the frequency plot, the usual x-cursor edit boxes show the cursor location as a fraction of the sample rate. The Xstring parameter is used to show this as an angular measure (in degrees) just to the right of the x-cursor readout. The usual y-cursor edit box shows the magnitude response in dB. The Ystring parameter is used to show this in linear form (in percent) just to the right of the y-cursor dB readout. Also note that the AxisLink parameter was used to unlink the magnitude and phase axes.
  • In the pole/zero plot, the usual x and y-cursor edit boxes show the pole/zero locations in Cartesian form. The Xstring parameter is used to show the polar form just to the right of the x-cursor readout.
  • Shows how to use axis('equal') to make the circle in the pole zero plot look true, even after the figure is resized.

weight.m

This script shows another example of putting more than one plot in a single figure. The SubPlot argument is used to create an upper and lower axis. The lower axis contains three traces showing the magnitude of 3 different weighting functions used in sound level meters (as defined by IEC 651). The upper axis also contains 3 traces showing the same 3 functions except in dB instead of the linear units used for the lower axis.
  • Normally plt only puts one trace on each subplot except for the main (lower) axis. So in this case (with 6 traces) plt puts 5 traces on the lower axis and one on the upper. Since we really want 3 and 3, a separate command is used to move two of the traces from the lower axis to the upper.
  • Note that when you cursor any trace in the lower axis, you get the y-axis readout in both linear and dB units, and the cursor in the upper axis automatically moves to the same trace and the same x position. The subplot argument usually will cause plt to do this synchronization, however since that only deals with a single trace for the subplots we have to use the 'moveCB' cursor callback (and case 1 of the switch command) to do this synchronization.
  • The traceID callback ('TIDcback') insures that the traceID box controls the upper axis traces as well (case 2 of the switch).
  • Note the 'LineWidth' argument in the plt call. This illustrates how any line property may be included in the calling sequence.

curves.m

This function shows an example where many GUI controls need to fit into a relatively small space.
  • The ten controls above the graph (nine edit text objects and one popup text object) all are used to control how the parametric curves in the graph are displayed.
  • If we used the traditional MatLab/Windows GUI objects, we would have had to make the graph much smaller to make room for all these controls.
  • In addition, the plt('edit') objects provide a much easier way to modify the numeric values, nearly matching the convenience of a slider object. The plt('edit') and plt('pop') commands are described here.

After starting curves.m, right click on the curve name at the bottom of the figure to cycle thru the 36 different cool looking curve displays. Left click on the curve name as well to select from the complete list of curves.

  • The equations in white just below the graph and above the curve name, serve as more than just the x-axis label. This specific string is evaluated by Matlab to compute the points plotted on the graph.
  • The vector t, and the constants a, b, and c that appear in these equations are defined by the controls above the graph. Experiment by both right and left clicking on these controls.
  • For the cases when more than one trace is plotted, the first control on the left (labeled "trace") indicates which trace is effected by the other nine controls above the graph.
  • Note that when you left click on a control, it will increase or decrease depending on whether you click on the left or right side of the text string.
  • Separate values for a, b, and c are saved for each trace of a multi-trace plot. This explains the variety of curves that can appear for a single set of equations (shown below the graph).

pub.m

This is a simple script which creates two plots in a format appropriate for publication (unlike the other examples which use a format appropriate for data exploration). The first is a large bar chart displaying the data embedded as comments in the code. The second (smaller) plot contains two traces with error bars.
  • Shows the use of the "+ - < > ." prefixes to modify properties of:
         + the left axis
         - the right axis
         < the left y-label
         > the right y-label
         . the x-label
  • Uses the 'COLORdef' parameter to select a white plot background
  • Uses text objects to create specially formatted tick labels
  • Uses the 'NoCursor' option to remove the cursor objects
  • Uses the 'TraceID','' parameter to remove the TraceID box

pltvar.m

To demonstrate the workspace plotter, this script creates several vectors in the workspace (including a structure containing two vector fields) and then starts the workspace plotter by calling plt with no arguments.
 

pltsq.m

This function shows one programming style for adding additional GUI controls to a plt window. Typically this is something you will want to do when creating an application based on plt.

  • Note how the 10 uicontrols are added to the figure (4 popups, 2 buttons, and 6 text labels).
  • Note that the uicontrol units are changed to normalized so that they reposition properly when you resize the plt figure window.
  • The 'moveCB' argument and the plt('rename') call are used to provide simultaneous cursor readouts for all 5 traces (using the TraceID). Click anywhere in the plot area to see the cursor values update)
  • Note the use of the 'AxisPos' argument to make room for the uicontrols added above the plot area.
  • Note the use of the 'Options' argument to turn off grid lines (initially) and to remove the y-axes Log selector from the menu box.
  • You can use the Erasemode popup to explore the effect of the erasemode property on drawing speed.

movbar.m

This function plots a series of 40 random bars and displays a horizontal threshold line which you can move by sliding the mouse along the vertical green line. As you move the threshold line, a string below the plot reports the number of bars that exceed the threshold. (This demonstrates the use of the plt xstring parameter.). These two buttons are added to the plt figure:
  • Rand: Sets the bar heights to a new random data set.
  • Walk: Clicking this once starts a random walk process of the bar heights. Clicking a second time halts this process. The Walk button user data holds the walk/halt status (1/0 respectively) demonstrating a simple way to start and stop a moving plot.

Note that you can move the threshold or press the Rand button while it is walking. Also, if you click on one of the vertical purple bars, the horizontal threshold bar will then follow the upper end of that vertical bar.
 

dice.m

This function displays a simulation of Sam Loyd's carnival dice game. - You bet 1 dollar to play (rolling 3 dice). If one six appears you get 2 dollars, if two sixes appear, you get 3 dollars, if three sixes appear, you get 4 dollars, and otherwise, you get nothing.
  • Three traces are created: accumulated winnings, earnings per bet, and expected earnings per bet.
  • The first two traces are displayed as they are computed, i.e. every time the dice are rolled, a new value is appended to the trace and the plot is updated so you can watch the function grow in real time.
  • A second axis is added near the top of the figure to show the dice. For each die, a line with dots as markers is added for each of the six faces, with only one of these lines being visible at a time. A square patch is also added for each die for the visual effect.

bounce.m

This function displays a specified number of markers with random shapes and colors in the form of a 5 pedal rose which then start randomly walking around bouncing off the walls. Click on the "Walk"/"Stop" button to start and stop the motion. The slider controls the walking speed. The argument determines the number of markers, i.e. bounce(88) will display 88 markers. If bounce is called without an argument, the default value will be used (120 markers).
  • Shows that plt can create many line objects (each marker is actually a line object) by using matrices for the x,y parameters.
  • Shows that plt can set line properties using cell arrays.
  • Shows how to disable TraceIDs to avoid the 99 trace limit.

circles12.m   

 

 

 

This is a two part script. The first part creates 3 figures each showing a different solution to the following problem ...
Draw 12 circles in a plane so that every circle is tangent to exactly 5 others.

The second part of the script draws the solution to the following problem ...
Divide a circle into n congruent parts so that at least one part does not touch the center. (Hint: the only known solution uses n = 12)
A slider is also added below the plot which lets you rotate the image and control the rotation speed.

  • Shows the utility of using complex numbers to hold the x and y positions of the plotted points.
  • Note that even though the calls to plt for solutions 2&3 specify same screen location ('Position' parameter) plt doesn't actually plot them on top of each other. Instead a small offset is added in this situation, a feature that makes it easier to create many plt windows so that any of them can be easily selected with the mouse.
  • Demonstrates two ways of creating the Trace IDs (reshape and repmat).
  • Demonstrates how to make circles look true by using a zero in the 'Position' argument (width or height).
  • Shows how to hide cursor objects and the menu box (second part of script)
  • Uses 'options','Ticks' to select axis tick marks instead of full grid lines.

wfall.m

This function shows one way to create a waterfall plot with hidden line removal.
  • Note how plt is called with a single trace color and no TraceID box.
  • Figure user data and setappdata are used to communicate with the callback.
  • Extensive use of the slider pseudo object to control the plotted data.