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:
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.
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!
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?
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.
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.
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:
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:
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.
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!
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.
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.
Recent Comments