Enhanced Matlab Plotting Series: Part II


Last post we defined the mPlot class and added properties to define the visual appearance of line objects.   A method to draw data on the current axes object was also added.  This time we are going to add some handy functions to make interaction with the mPlot object easier.

Interacting With plotCounter

As we saw last time, plotCounter is used to determine which visual styles to apply to the current line.  Recall from last time that we had to increment the plotCounter property external to the class….as shown below.

Instead, let’s build 3 methods into our class that increment, decrement and reset the value of plotCounter.

While we’re on this topic, add one property named isAutoIncrementing.  This will be a boolean value, either 1 or 0.   If the value of isAutoIncrementing equals 1, a method will automatically increment plotCounter each time a line is drawn to an axes.    Notice below that we had to add an if-then statement within the plot function to increment plotCounter if auto-incrementing is enabled.

This property will also need to be initialized within the constructor of mPlot.  This will eliminate the need to call an increment method every time we plot the next line.  Of course, there may be some cases where you don’t want this type of functionality.  In that case simply set isAutoIncrementing to zero and you’re back in full control.

plotCounter Methods in Action

The short lines of code below will be used to test out the new methods within mPlot.  Check out the comments for more information….but all we have done here is 1) plot on an axes while manually incrementing, 2) reset the plotCounter to zero, 3) enable auto-incrementing and 4) plot on a different axes.

After executing the code the plots below are produced.

So regardless of technique, both produce the same result.  But when plotting multiple lines on a single axes, auto-incrementing can be advantageous.  One may even choose for this to be the default.  In this case, simply set isAutoIncrementing to 1 inside of the mPlot constructor.  Then each time you instantiate mPlot auto-incrementing will be enabled.

Next Time

In the next part of this series we will extend formatting beyond lines; we will dive into altering the appearance of the axes and figure.

Advertisements
This entry was posted in Matlab Plotting. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s