Doug's MATLAB Video Tutorials

December 18th, 2008

MATLAB example: Don’t reinvent the wheel

I love a good visualization of data, and often cringe when I see data in a table that would be much more effective as a graph. As with many people in Boston today, I am trying to decide what to do about the impending snow storm tomorrow. Last year in a similar storm, my 20 minute commute became a six hour ordeal.

That brought me to Boston.com for the weather outlook. I found this chart:

MATLAB can do this better

Now, I wanted to see what this would look like as a graph. I thought it would be cute to do an actual thermometer. That just struck me as something that someone else would have wanted to do already, so I went to MATLAB Central File Exchange. One search later brought me to the file I wanted.

This file would produce output like this:
MATLAB thermometer

I wanted something like this though:
MATLAB Thermometer

In the original, you could not get ranges to overlap, so I needed to use the individual data points. The problem was that by default, all the text would be to the right. However, the subfunction that drew this text took in input to say which side to to put the text!

MATLAB Code

So Eric had thought ahead. However, the code that calls this always sets left to false.

MATLAB code

I was looking for a quick and dirty way to do this visualization, so I set that flag like this:

MATLAB Code
Now, when the string I am using has “High” in it, I will get what I want. I did a little constant tweaking to get the text a little farther left and I was done.

The lesson here is that when using MATLAB, there is very often someone else that has made some code that you could use. It might be one of our toolboxes, or it might be something that another MATLAB user made. It reminds me of a great quote from grad school:

“Two weeks in the lab will save you five minutes in the library.”

Once you have that code, feel free to modify it for your specific needs like I did. That is why it is there.

2 Responses to “MATLAB example: Don’t reinvent the wheel”

  1. Kenneth Eaton replied on :

    When I heard you start talking about making a plot that looked like a thermometer, the first submission I thought of was UITANK from Elmar Tarajan:

    http://www.mathworks.com/matlabcentral/fileexchange/19511

    Both this post and the one you cited are impressive. The FEX can have some very nice surprises sometimes!

    Ken

  2. Scott Hirsch replied on :

    Now my old-fashioned thermometer is looking pretty lame :(

    http://www.mathworks.com/matlabcentral/fileexchange/3154

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.