pBook‎ > ‎Matlab‎ > ‎

Class Example

posted Sep 11, 2014, 8:49 AM by Javad Taghia   [ updated Sep 11, 2014, 8:49 AM ]
Source: http://matlabmafia.wordpress.com/2010/11/27/enhanced-matlab-plotting-series-part-i/

We all know about Matlab’s unparalleled plotting capabilities.  The signature 3-D surface plot sitting on our desktop as a Matlab icon is a daily reminder.  The intent here is not to delve into surface or mesh plots, but to focus on simple 2-D plotting.

A Brief Background

First let me give a little background about how this plotting layer came about.  As an avid Matlab user, I spend most of my days staring at monitors filled with m-files and plots. Over time I began to realize that I preferred all of my plots to appear a certain way; and was spending much of my time formatting figures, axes and lines.  So I decided to develop an engine within Matlab that would handle all of my plotting. You can think of this as a layer between the coder and the low level Matlab plotting and formatting functions.  Almost immediately after creating this layer I recognized that I was substantially more efficient, and to top it off everything appeared exactly the way I wanted; too good to be true, right?.  After working with and developing this layer for some time now, I’ve added a substantial amount of functionality….I’m at the point where I couldn’t imagine having to revert back to interfacing directly with the low-level Matlab plot function.  So, in this series of articles I hope to explain what this magic “layer” is that I’ve been referring to.

So What is This Layer???

Before diving in, I must warn you that this “layer” I’ve been referring to is actually called a class.  Classes are used in a form of programming termed Object-Oriented Programming (OOP).  But don’t shy away simply because you haven’t used them.  We will walk through how the class is created to some extent.  I was brought up in Mechanical Engineering (ME) and know that ME’s (and other engineering disciplines) are rarely exposed to object-oriented programming (as I usually get the blank stare when I bring it up around my ME colleagues).  If you are interested, check out Matlab’s website about OOP @

http://www.mathworks.com/products/matlab/object_oriented_programming.html.

They do a nice job of providing an introduction and explaining the syntax.  Since OOP is something foreign to many branches of engineering, Matlab OOP may be one of our series in the near future.

Our Basic Plotting Object

Ok, so let’s get started.  In this first part of the series we are merely going to predefine a color scheme for our plots; specifically, line and axes properties.  It sounds simple, and is…but I have found this to be very useful for daily Matlab chores.

Let’s first begin by creating a blank class with an empty constructor (a function that runs when you instantiate the class), a few properties (attached variables local to our class) and one method (aka function).

Next lets add some properties to our class.  These properties will be used to define the style of a Matlab line object.  The comments in the code explain the intent of each property.

For now, just set GetAccess and SetAccess to public.  We will discuss what this means later in the series.  Also, the use of these properties may be a bit fuzzy at this point.  Keep going and this will become clear as we flesh out these properties.

Adding the Plot Method

Now lets incorporate a method (function) used to plot data.  This function will behave similar to Matlab’s plot function in that it accepts x and y vector data pairs as input arguments.  This plot function will draw the data onto the current axes while assigning the line properties defined within the class.

Defining Line Settings (Visual)

Now all we have to do is define a few line properties and the class should be ready for action.  For this task we will define a function that builds our color, marker and line type arrays.  This will then be called within in the constructor of the class.  Hence once the class is instantiated these properties of the class will get defined.  This is shown below.  We have defined unique properties for 4 different lines within the buildProperties function.  Notice the last statement initializes the plotCounter property to zero.

Putting the Plot Object to Work

Let’s try out the new mPlot class.  As shown below, instantiation of the class creates an instance of our mPlot object.  All of the properties that we have defined within the class definition file show up within the object.

Next test out the plot  method.  The simple loop

should produce

ċ
mPlot.zip
(5k)
Javad Taghia,
Sep 11, 2014, 8:49 AM
Comments