File Exchange Pick of the Week

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

July 2nd, 2009

linkzoom

Bob's pick this week is linkzoom by Carlos Adrian Vargas Aguilera.

The following plot shows temperature versus depth in two sets of units.

Z = linspace(0,150)';           % Depth in meters
TC = -tanh((Z-30)/20)+23;       % Temperature in °C
dc2df = @(dc) (9/5)*dc + 32;    % °C->°F
mt2ft = @(mt) mt/0.3048;        % meters->feet

ax(1) = axes('YDir','reverse',...
             'Box','off',...
             'position',[0.13 0.11 0.77 0.78]);
line(TC,Z,'parent',ax(1))
axis tight
xlabel('Temperature, °C')
ylabel('Depth, m')
ax(2) = axes('Position',get(ax(1),'position'),...
             'HitTest','off',...
             'XAxisLocation','top',...
             'YAxisLocation','right',...
             'YDir','reverse',...
             'XLim',dc2df(get(ax(1),'XLim')),...
             'YLim',mt2ft(get(ax(1),'YLim')),...
             'Color','none');
xlabel(ax(2),'Temperature, °F')
ylabel(ax(2),'Depth, ft')
linkzoom(ax)

The different units of measure are handled using a second axes. If you want to zoom and pan the plot, by default the two axes would not stay synchronized. MATLAB offers a very useful linkaxes command. That works great when plots share common X and/or Y coordinate values. In this case linkaxes would do the wrong thing. So Carlos created linkzoom. Once turned on, you can pan and zoom and both measurement scales stay in lock step. Download the submission and run this code in MATLAB to see for yourself.

Comments?


Get the MATLAB code

Published with MATLAB® 7.9

June 26th, 2009

“Don’t let that INV go past your eyes; to solve that system, FACTORIZE!”

Jiro's pick this week is FACTORIZE by Tim Davis.

If you browse through the MATLAB Newsgroup, you will occasionally find discussions on "matrix inverse". In many of those discussions, the ultimate goal of wanting matrix inversion was to solve a linear system

   Ax = b

Take this simple example:

A = randn(3, 3)
b = randn(3, 1)
A =
      0.52006     -0.79816     -0.71453
    -0.020028       1.0187       1.3514
    -0.034771     -0.13322     -0.22477
b =
     -0.58903
     -0.29375
     -0.84793

Technically, you can solve it using the INV function.

x1 = inv(A)*b
x1 =
      -30.333
      -56.674
       42.054

But as the documentation for inv suggests, there is a better way to solve this problem, both in terms of efficiency as well as accuracy. That is to use the backslash operator:

x2 = A\b
x2 =
      -30.333
      -56.674
       42.054

Tim takes this one step further and extends the capability with his Factorize object. One of the benefits is that you can "reuse" this efficiency in different conditions:

c = randn(3, 1);

F = factorize(A);
x3 = F\b
x4 = F\c;
x3 =
      -30.333
      -56.674
       42.054

This is a simple 3-by-3 example for illustration, but you'll appreciate its power when you're working with larger systems. I like this entry for several reasons.

  • He uses the new MATLAB Class system (introduced in R2008a). Specifically, he utilizes object-oriented techniques, such as property attributes and abstract classes.
  • He includes a very thorough, pedagogical document that explains the uses of the object and the theory behind different techniques for solving linear systems.
  • He includes a test suite for testing the accuracy, performance, error-handling, and display methods of the Factorize object.

I recommend looking at this highly-rated entry by Tim.

Comments?


Get the MATLAB code

Published with MATLAB® 7.8


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

  • Jon: This is a useful tool - but in fairness I have to point out that it is remarkably similar to a routine of the...
  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?

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