File Exchange Pick of the Week

September 11th, 2009

Tables made easy

Jiro's pick this week is DataTable by Paul.

MATLAB stands for MATrix LABoratory. So it's no surprise that we work with a lot of matrices. From time to time, I've wanted to embed a matrix in a Wiki page or an HTML document. The problem is that it's a tedious process to create the appropriate markup, and it doesn't scale well for large matrices.

Paul's DataTable class makes this extremely simple. Just create your matrix like a cell array, and convert it to the appropriate markup with a single command. In addition, he uses the new MATLAB Classes (introduced in R2008a) to implement his class.

Let's take a look at an example. Oh, and here is some personal info about us.

table         = DataTable();
table{2:4, 1} = {'Bob'; 'Brett'; 'Jiro'};
table{1, 2:5} = {'Occupation', ...
  'MathWorker since...', ...
  'FEX submissions', ...
  'POTW posts'};
table{2, 2:5} = {'Quality Engineer', 'July 2001', 24, 25};
table{3, 2:5} = {'Application Engineer', 'Dec 2005', 47, 27};
table{4, 2:5} = {'Application Engineer', 'May 2006', 32, 23};

% Center-align the text
table.setColumnTextAlignment(1:5, 'c')

table.toText();
|       |      Occupation      | MathWorker since... | FEX submissions | POTW posts |
|  Bob  |   Quality Engineer   |      July 2001      |       24        |     25     |
| Brett | Application Engineer |      Dec 2005       |       47        |     27     |
| Jiro  | Application Engineer |      May 2006       |       32        |     23     |

For HTML printing, simply call the toHTML method. You can even add table attributes!

table.toHTML('tableattributes', 'bgcolor="#ffcc00"');
<table bgcolor="#ffcc00">
<tr>
  <td align="center"></td>
  <td align="center">Occupation</td>
  <td align="center">MathWorker since...</td>
  <td align="center">FEX submissions</td>
  <td align="center">POTW posts</td>
</tr>
<tr>
  <td align="center">Bob</td>
  <td align="center">Quality Engineer</td>
  <td align="center">July 2001</td>
  <td align="center">24</td>
  <td align="center">25</td>
</tr>
<tr>
  <td align="center">Brett</td>
  <td align="center">Application Engineer</td>
  <td align="center">Dec 2005</td>
  <td align="center">47</td>
  <td align="center">27</td>
</tr>
<tr>
  <td align="center">Jiro</td>
  <td align="center">Application Engineer</td>
  <td align="center">May 2006</td>
  <td align="center">32</td>
  <td align="center">23</td>
</tr>
</table>

The above HTML markup renders to the following table:

Occupation MathWorker since... FEX submissions POTW posts
Bob Quality Engineer July 2001 24 25
Brett Application Engineer Dec 2005 47 27
Jiro Application Engineer May 2006 32 23

Thanks for this very useful and well-written entry, Paul!

Comments?


Get the MATLAB code

Published with MATLAB® 7.9

September 4th, 2009

Concatenate vectors of unequal lengths

Brett's Pick this week is padcat, by Jos--one of the File Exchange's most popular contributors.

MATLAB has a few containers that are useful for storing heterogeneous data. In particular, one can store just about any combination of variables or data types in cell arrays, structures, or (if you have the always useful Statistics Toolbox) dataset arrays. Sometimes you want something lighter-weight, and easy to manipulate. If you just wanted to combine several row or column vectors into a matrix, the function cat makes short work of it, if the vectors are the same length and orientation (all row vectors or all column vectors). If, on the other hand, they had the same orientation but different lengths, cat wouldn't know what to do with them.

With padcat, you could automatically combine them, padding shorter vectors with NaNs as necessary.

Say, for example, that you had four row vectors of different length:

a = 1:5 ; b = 1:3 ; c = [] ; d = 1:4 ;

and you wanted to create from them a single matrix. Try padcat:

M = padcat(a,b,c,d) % all are row vectors
M =
     1     2     3     4     5
     1     2     3   NaN   NaN
   NaN   NaN   NaN   NaN   NaN
     1     2     3     4   NaN

As an added bonus, Jos's function can generate a second output that contains a binary mask showing true (or 1) where elements of M originated from an input vector, and false (or 0) where they were padded. This can be useful if any of the component vectors themselves contain NaNs. For instance:

a = [1:3]' ; b = [] ; c = [1;NaN] ;
[M,tf] = padcat(a,b,c) % all are column vectors
M =
     1   NaN     1
     2   NaN   NaN
     3   NaN   NaN
tf =
     1     0     1
     1     0     1
     1     0     0

(Note that the second element in the third column of tf [i.e., row 2, column 3] is 1, indicating that the NaN in that position of M originated in vector c.)

Just a nice utility function to have around. Thanks, Jos!

Comments?


Get the MATLAB code

Published with MATLAB® 7.8

August 28th, 2009

A great utility for debugging

Brett's Pick this week is dbmute and unmute, companion functions that facilitate code tweaking and debugging.

Those of you who read my colleagues' blog Ken & Mike on the MATLAB Desktop may have already been introduced to today's pick; it comes to us from fellow MathWorker and blogger Ken Orr (his first official submission--welcome to the File Exchange, Ken!).

Back in mid-July, Ken wrote a blog post on Muting Breakpoints. He described how he uses breakpoints to help him debug his code, and he wrote that he wanted the ability to quickly disable (and re-enable) those breaks. To solve the problem, Ken wrote, and showed the code for, dbmute and dbunmute. I adopted the functions immediately--they've quickly become an integral part of my own code development workflow. I also commented on that post, suggesting that these functions belong on the MATLAB Central File Exchange, and hinting about their destiny as "Picks of the Week." Good to my word, today's Pick honors Ken's submission.

One side note: Ken suggested in a final thought that it might be useful to create shortcuts to the functions:

I took it one step farther, and implemented a single shortcut that toggles my breakpoints:

Anyone care to venture a guess how I did that?


Get the MATLAB code

Published with MATLAB® 7.8

August 21st, 2009

Close that Dialog!

Jiro's pick this week is AutoWarnDlg by Jan Simon.

By this time, some of you may know that I am attracted to visual tools. I was browsing through the File Exchange last week, and with this post by Ken & Mike fresh in my mind, Jan's entry caught my attention.

When I was in school, I was performing a large parameter study on a lengthy simulation. Back then, there was no Parallel Computing Toolbox so I ended up running computations hours at a time. In order to provide some visual feedback, I used dialog boxes to display the status every hour. I also wanted to be able to stop the run at those times, but if I happened to be away from the desk, I wanted it to continue. Jan's AutoWarnDlg would've done the job.

Along with a flashy, blinking icon, his dialog box has a nice auto-select feature. Now, you can create warning dialogs that would go away automatically if unattended.

Comments?


Get the MATLAB code

Published with MATLAB® 7.8

August 14th, 2009

Self-Balancing Wheeled Robot

Jiro's pick this week is Self-Balancing Robot by Yorihisa Yamamoto.

This one comes from Tokyo, Japan. I am currently here for a few weeks working with the Application Engineers at the MathWorks Japan office which just opened this July. These engineers have dealt with MathWorks products for many years as our local distributor, and they're all very sharp people. Yorihisa is one of the Application Engineers here in Japan, and he focuses on Simulink and controls applications.

Being a MATLAB person, I was blown away when Yorihisa showed me this self-balancing lego robot where the control algorithm was developed in the Simulink environment. My eyes were glued to this wheeled robot swaying back and forth to keep its balance. See it in action here:

I'm curious to hear about other hardware interface demos/projects people have done using MATLAB and Simulink. Please tell us about it here. If you have corresponding File Exchange entries, even better!


Get the MATLAB code

Published with MATLAB® 7.8

August 7th, 2009

StructDlg (Part 2 of 2)

Bob's pick this week is StructDlg by Alon Fishbach.

This is a follow-on from last week's pick.

Do you build MATLAB applications with a graphical user interface (GUI)? Do want to provide your end users with rich, powerful dialogs to manage their work flow? Would you prefer to not start from scratch? Then StructDlg may be for you. Be sure to review Alon's examples for a quick tour of the many capabilities he implemented. I can see using this to tweak model parameters or manage user preferences for starters.

How could StructDlg make your MATLAB program more user friendly?

Comments?


Get the MATLAB code

Published with MATLAB® 7.9

July 31st, 2009

STRUCTDLG (Part 1 of 2)

Bob's pick this week is STRUCTDLG by Marco Cococcioni.

This is a nifty data editing tool. Suppose you have a structure with a few named fields in your MATLAB workspace.

myStruct.Submission = 'Time index';
myStruct.Author = 'Bob';
myStruct.Address = 'http://wwww.mathworks.com/matlabcentral/fileexchange/21891'
myStruct = 
        Author: 'Bob'
    Submission: 'Time index'
       Address: 'http://wwww.mathworks.com/matlabcentral/fileexchange/21891'

Notice the address contains a typo. I could correct it by modifying the code but suppose the data was loaded from a file, or typed by an end user (possibly you). What structdlg provides is a simple tool for editing the values of structure fields.

myStruct = structdlg(myStruct);

myStruct
myStruct = 
        Author: 'Bob'
    Submission: 'Time index'
       Address: 'http://www.mathworks.com/matlabcentral/fileexchange/21891'

If you build programs in MATLAB for others, this could be an instant and effective plug-in for your applications.

I also want to thank S C for his 07 Nov 2005 rating comment.

"This utility is very useful and is very simple and intuitive.

When a more powerful structdlg utility is needed I could be preferable to look to the structdlg version provided by Alon Fishbach (you can find it in this forum, also)."

Indeed! Next week I will highlight Alon's submission so be sure to check back. Meanwhile, don't be shy about sharing your comments.


Get the MATLAB code

Published with MATLAB® 7.9

July 24th, 2009

Using multiple colormaps in a single figure

Former "Pickmaster" (and current blogger) Doug Hull suggested that John Iversen's color freezer might be "Pickworthy." I wholeheartedly agree.

Suppose you wanted to display the image of a clown that ships as a MATLAB demo. This snippet would do the trick:

load clown;
image(X);
colormap(map);

Similarly, if you wanted to display a penny with a copper colormap, this would work:

load penny.mat
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat

But what if you wanted to show the two graphics in one figure window? Colormaps are properties of figures, so it's a bit more complicated to do that than you might think:

figure('color','w')
subplot(1,2,1)
image(X);
colormap(map);
subplot(1,2,2)
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat

Notice that the second call to colormap affects the image of the clown as well as that of the penny. There are ways to circumvent this behavior. Notably, you could combine the two colormaps into one, and then use different portions of the concatenated colormap for each graphic displayed. Or, if you have the Image Processing Toolbox and are dealing exclusively with images, you can use function subimage to visualize images with different colormaps.

But John's submission makes it considerably easier to combine graphics; just issue a freezeColors command after drawing the clown image, then generate the display of the penny:

figure('color','w')
subplot(1,2,1)
image(X);
colormap(map);
% Here's John's contribution:
freezeColors

subplot(1,2,2)
contour(P,15)
colormap(copper)
axis ij square
pcolor(P)
axis ij square
shading flat

I've been meaning to write a similar function for years. Now I don't have to. Thanks, John!

Comments? Leave them here.


Get the MATLAB code

Published with MATLAB® 7.8

July 17th, 2009

Want to get rid of your code comments?

Brett's Pick this week allows you to strip comments from your code, should you need to do so.

Several years ago--while building several large, multi-file applications--I found myself getting unwanted output scrolling to my command window. Clearly, I had omitted some line-terminating semicolons somewhere in my morass of code. Finding these instances was tedious and time-consuming (and a task that MLINT now makes trivially easy).

I decided to write a function to detect missing semicolons, but quickly found that coding to ignore comments was beyond my capacity--at least at the time.

Fortunately, Peter Acklam (whom you may know as a frequent contributor to the fine CSSM newsgroup, and as a MATLAB vectorization guru), came to my rescue and, in response to my queries, posted his Comment-Stripping Toolbox.

I haven't had to use my missingsemicolons function in quite a while, but every now and then I still have occasion to strip comments from some MATLAB code. And when I do, I still turn to Peter's submission.

The Comment-Stripping Toolbox allows you to strip comments from a string, or from a file specified by name or by file I.D. One of the things that impressed me the most about Peter's submission was that it reflected his mastery of regular expressions:

If you ever need to strip comments, or just want to see some impressive code, try out Peter's file, and let us know about it here.


Get the MATLAB code

Published with MATLAB® 7.8

July 10th, 2009

addaxis

Bob's pick this week is addaxis by Harry Lee.

First, I'd like to thank Andres for drawing my attention to this submission with his comment to last week's pick, linkzoom.

What addaxis provides is a way to overlay plots. Suppose you want to compare a set of curves. They share common units along the X axes (ie, time) but their Y scales are different. Plotting them on the same axes may not be useful. Using a number of subplots may be undesirable as well. Harry's solution might be something to consider.

This submission is a mini toolbox of functions. The example above uses addaxis, addaxislabel and addaxisplot. Just type "help addaxis" at the MATLAB command prompt. You might also check out his Readme.txt file to get started.

This type of plotting capability is very specialized which is reflected in his design approach. If it doesn't meet your exact needs, just modify it. Sure beats starting from scratch!

Comments?


Get the MATLAB code

Published with MATLAB® 7.9


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Brett: Hi Eric, Thanks for the questions. No, my icon doesn’t change colors; I don’t think there’s...
  • Eric: Brett, Also, do you need to be an Admin on the machine for setappdata(0,'dmbmut e',true); to work?
  • Eric: Brett, Does you shortcut icon change depending on the mute status - green when unmuted, red when muted?
  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify

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