# File Exchange Pick of the Week

## Clear Everything!

Jiro's picks this week are ccc by Chad Greene and clr by Brenden Epps

Yes, this week's picks are these 3 lines of code:

clear all
close all
clc


Is this a joke?! No, not really.

In my day-to-day job as an application engineer, I show how MATLAB can be used to solve people's problems. When I'm in front of customers, I do demo after demo, switching context left and right. I need a quick way to reset everything so that I can show the next thing they need to see. I need to run the three commands above frequently during any single customer engagement.

ccc and clr both accomplish this with a short, single command. It seems very trivial, but it's probably one of the most used commands in my job. I also use the toolstrip and shortcuts often, so I have a shortcut for ccc.

The code behind it is

Note that I actually use "close all force" to close any hidden figures.

By the way, there's an interesting discussion about the use of "clear all" in the comments section. Personally, I think it's okay as long as you know what it does, and in my case it makes sense for my day-to-day work.

Tell us about your simple shortcut commands you created to make your life just a little easier. Also, if you do have a command or a shortcut for clearing everything, what do you call it? My colleagues Sean calls it cll and Brett calls his purge. Feel free to leave a comment for Chad or Brenden as well.

Get the MATLAB code

Published with MATLAB® R2013a

## Interactively measure features in images

Brett's Pick this week is "Image Measurement Utility", by Jan Neggers.

Those of you who have followed my Picks over the years (or ever downloaded one of my own File Exchange submissions, or read my guest posts on Steve's image processing blog) know that I am passionate about image processing using MATLAB. I often get to help customers automate the extraction of information from multiple images; when the images are amenable, MATLAB is an excellent platform for creating such automation routines.

Often, though, the features in an image series are difficult to detect automatically and reliably, and automating analyses is quite difficult. In such cases, we need to use manual tools to get the information we need. The Image Processing Toolbox has resources (like imdistline) that facilitate some of those measurements, but it's generally left to the user to do things like scale the measurements, or calculate angles.

Enter "Image Measurement Utility."

Jan's tool allows you to drag to define a calibration line, which you can propagate through a series of images. Then, you can readily measure (in real units): distances between points, or between a point and a line; angles; circle radii; or distances along smoothed or piecewise-linear paths. (You can also measure the mean pixel intensity underneath a manually drawn path. This functionality was added by Jan at the request of a user.)

I really like the fact that Jan provided his measurement tool in "standard form" (that is, as a collection of MATLAB files callable from the MATLAB Command Line), and as an app as well. His documentation is also quite good, and describes all the things you can do with the tool. And, I think it's great that Jan was responsive to the users of the tool, and amenable to improving it based upon the feedback he received.

Very handy untility, Jan! I'm sure I'll have lots of opportunities to use it.

As always, I welcome your thoughts and comments. Or leave feedback for Jan here.

Get the MATLAB code

Published with MATLAB® R2013a

## Dynamical Systems with Applications using MATLAB

Will's pick this week is Dynamical Systems with Applications using MATLAB by Stephen Lynch.

Many years ago, a professor of my numerical methods class insisted that we write all our programs in C. When several students bemoaned this imposition, he explained that the reason he did this was that the exercise was "pedagogical." Well, once I looked up what pedagogical meant, I saw some merit to his position. It never hurts to flex your skills in a different programming language. In this case, however, I felt like C was the wrong tool for the job. MATLAB was a more natural environment to develop the algorithms in question, and its output still satisfied the homework deliverable requirements. In the end, the TA accepted my work from MATLAB without complaint.

So anytime I see a professor arrive at the similar conclusion that MATLAB is an ideal programming platform for engineering problems, I can't help but be delighted. Dr. Lynch has in fact built his entire dynamics textbook around MATLAB and Simulink. All the examples he uses in the book are available on MATLAB Central. Some 59 MATLAB files and 10 Simulink models are provided.

Of course if you have the book, you'll have a better appreciation for what intriguing plots like these are conveying.

Nevertheless, anyone with some experience in dynamics will appreciate the work that's put together here.

## Multi-line Comet Plot

Jiro's pick this week is multicomet by Ben.

Did you know that we had a function called comet that allows you to animate trajectory plots? It was like a hidden gem for me when I found it. It's a simple concept, but very useful for when I wanted to animate my signal trace. There was no need for me to write loops to update my graphics. With the new toolstrip, you can find it right away.

Ben extended comet to work with multiple trajectories. Now it's easy to visualize multi-state dynamics, such as the Lotka-Volterra predator-prey model.

$$y_1' = (1 - \alpha \cdot y_2) \cdot y_1$$

$$y_2' = (-1 + \beta \cdot y_1) \cdot y_2$$

The prey population increases when there are no predators, and the predator population decreases when there are no prey. Let's use the example function lotka that uses $\alpha = 0.01$ and $\beta = 0.02$.

type lotka

function yp = lotka(t,y)
%LOTKA  Lotka-Volterra predator-prey model.

%   Copyright 1984-2002 The MathWorks, Inc.
%   $Revision: 5.7$  $Date: 2002/04/15 03:33:21$

yp = diag([1 - .01*y(2), -1 + .02*y(1)])*y;


% Define initial conditions.
y0 = [20; 20];

% Simulate the differential equation.
[t,y] = ode45(@lotka, 0:0.005:15, y0);

% Animate.
multicomet(t, y)


Let's try different initial conditions.

y0 = [200; 200];

[t,y] = ode45(@lotka, 0:0.005:15, y0);

multicomet(t, y)


Thanks, Ben, for this nice extension to comet!

Get the MATLAB code

Published with MATLAB® R2013a

## Spirograph!

Brett's Pick this week is "Spirograph GUI", by Brian Moore.

Just cool fun. You gotta try this!

As always, I welcome your thoughts and comments. Or leave feedback for Brian here.

Get the MATLAB code

Published with MATLAB® R2013a

## June 14th, 2013

Greg's pick this week is Dynamic Mask Field Changing From Selected Pulldown Menu by David Manegold.

Ever wanted to change how your Simulink block mask appears depending on certain parameter conditions of the mask? David has a nice example.

I selected this submission because it demonstrates an advanced Simulink development technique in a straight-forward manner.

### Contents

#### Change Thy Appearance, and Quick!

Masks have the ability to change what parameters are available depending on which parameters have been selected. In this example, if the value of the "Selector" parameters is "A" then the set of parameters associated with "A" appears in the mask. But if you select "C" instead, then a new set of parameters is displayed. Also option "C" has a subset of parameters that can also change depending on what you choose for "C alt field type 3".

#### What Doest Thou Hide Behind the Mask?

The mask is a dialog box that appears when you double-click on a block in Simulink. It provides a means to enter parameters for a Simulink block or subsystem to affect its functionality. This means you can have two instances of a block or subsystem in the same model that exhibit different behavior depending on the parameter values entered in the mask. You can create your own masks for subsystems or blocks by right-clicking on the block and select

David leverages two capabilities to enable this behavior in a block mask.

2. The MaskVisibilities property of the block

In the mask editor, if you select a parameter to edit, you can populate the "Dialog callback" field to execute MATLAB functions that get executed when the parameter is changed in the block mask.

Within the callback function ChangeMaskFields, David changes the visibility of specific parameters based on the value of the "Selector" parameter. He defaults the parameter visibilities to "off":

[M{:}]=deal('off');


except for the first parameter (which is the Selector parameter):

M{1}='on';


Then he enables specific parameters based on the value of the "Selector" parameter:

[M{i0:ie}]=deal('on');


Get the MATLAB code

Published with MATLAB® R2013a

## Plot (real) Big

Jiro's pick this week is Plot (Big) by my co-worker Tucker McClure.

"Big data"

That seems to be the buzz lately. But when people say "big data", it could mean a lot of things.

• Storing big data
• Saving big data
• Visualizing big data
• Analyzing big data
• Manipulating big data
• etc.

This entry by Tucker is all about "visualizing" big data. This was a recommendation from another one of my co-workers, Adam.

Visualizing Big Datasets

One of the issues with visualizing big datasets is that you have so many points displayed on the screen, and exploring (zooming and panning) may become sluggish. It's nice that you have all of those points available, but your screen has only so many pixels, and you don't get additional information by putting more points than what the screen is able to display.

So what can you do? Well, you can choose to display fewer points, just enough to give you a sense of what the dataset looks like. But then, you'd like to be able to see the details if you zoom in. It's sort of like Google Maps, for instance; when you're zoomed out, you see a rough outline of the city and some major highways. When you zoom in, you start to see some of the local roads.

Can we do something like this with MATLAB plots? I created something like that for line plots. Doug even wrote a Pick on it. However, that was more of a proof-of-concept that worked for only 2D line plots. Tucker went all the way. He implemented this using MATLAB Classes, and it could be applied to other plot types. You could also call it with additional arguments that you might pass into these plotting commands. Here are some additional things I like about Tucker's entry:

• Nice documentation for the class
• An extensive set of examples
• A convenience function reduce_plot which allows you to use this class like a function, returning handles to the plot
• A companion class for converting your figure to an interactive explorer, which allows you to zoom and pan by using your scroll wheel and left-click dragging, respectively

Here's a quick animation of reduce_plot in action, compared to a regular plot. You can see the difference in responsiveness of the plot when I am trying to pan the plot.

Thanks for an awesome entry, Tucker. And thanks for the recommendation, Adam. Some swag going to both of you!

We'd like to hear from you if you do, or are interested in, big data visualization. What kind of visualization do you use? Do you use any special techniques to deal with your big data? Give this a try and let us know what you think here or leave a comment for Tucker.

Get the MATLAB code

Published with MATLAB® R2013a

## Nominate your favorite NEW File Exchange submission

So, it's been just over a year since I last asked readers to nominate their favorite (formerly unrecognized) File Exchange submissions. (My, how time has flown!)

Since mid-April, when I issued that challenge, MATLABbers around the world have shared approximately 3100 new files--and that doesn't include files that were simply modified in that timeframe. That means that more than 22% of the 14000 files currently on the Exchange didn't exist a year ago. And there's some great new stuff out there. Heck, there's even a "flatulence simulator." (I think I'll send Maxim some swag just for having posted that!)

So...time to reissue the challenge, but this time with a twist:

Nominate your favorite new submission--let's say, with a submission date (not modification date) of April 12, 2012 or later. If we agree, and if we feature your nomination in this blog, we'll send both you and the file's author some cool MATLAB swag.

The same ground rules apply:

• Please only suggest files that are covered under the BSD!
• Submissions should be exemplary for some reason that you can point out. Is it just beautifully written? Have you found it exceptionally useful? Great use of visual elements? (Tell us what it was that led you to select a particular file. We may even quote you!)
• Your nomination constitutes your acknowledgment that we may quote you, and your permission to do so.
• Please don't suggest any files that have already been Picked. (All previous Picks of the Week are tagged on their entries with a POTW stamp.)
• Please consider files that do not feature capabilities undocumented in MATLAB. (We know they can be useful, but then they just might break with the next release!)
• Remember: cool MATLAB swag to anyone who steers us to a file we use!

I think we'll make this an annual event. Thoughts?

Get the MATLAB code

Published with MATLAB® R2013a

## Delta Sigma Toolbox

Idin's pick for this week is the Delta Sigma Toolbox by Richard Schreier.

Delta-Sigma (or sigma-delta) modulators are commonly found in electronic components such as analog-to-digital and digital-to-analog converters (ADCs and DACs), and increasingly in [fractional-N] frequency synthesizers (PLLs), and switch-mode power supplies. Generally speaking, a delta-sigma modulator produces a highly over-sampled binary signal at its output (0/1) which can be low-pass filtered to reproduce the input signal. Figure 1 below shows an example input & output signal. To read more about delta-sigma modulators, go here, here, and here. Richard’s textbook is also a good resource: "Understanding Delta-Sigma Data Converters" by Schreier and Temes (ISBN 0-471-46585-2).

There is a large body of literature dedicated to analysis and design of delta-sigma modulators, and the Delta Sigma Toolbox provides a great tool for analyzing these components in MATLAB. The toolbox contains a fairly complete list of well documented functions to construct, analyze, and simulate delta-sigma modulators of arbitrary order. The full documentation for the toolbox is provided in DSToolbox.pdf (part of the download).

To start using the toolbox, I highly recommend studying the provided example and demo files (dsdemo1-8.m and dsexample1-4.m). I like dsdemo2.m as a starting tutorial. It begins by creating a noise transfer function (NTF), and then simulates the modulator with an example sinusoidal input:

OSR = 32;
H = synthesizeNTF(5,OSR,1);
N = 8192;
fB = ceil(N/(2*OSR)); ftest=floor(2/3*fB);
u = 0.5*sin(2*pi*ftest/N*[0:N-1]);  % half-scale sine-wave input
v = simulateDSM(u,H);


The time domain output shows the sinusoidal input (red) and the binary output (green).

The spectrum of the output can also be easily computed using MATLAB's FFT function:

This figure looks like the superposition of two signals: the spike at ftest/N (0.0104 in this case) which represents the input sinusoid, and some high frequency "noise". The high-pass noise is in fact one of the desirable properties of delta-sigma converters; these converters shift the quantization noise to high frequencies, i.e., away from our signal/carrier of interest. It should be self-evident from Figure 2 that a simple low-pass filter of this output signal would yield the original sinusoidal input.

The next few lines in dsdemo2.m compute the SNR on the output signal, and also use Delta Sigma Toolbox functions to compute and display the expected (theoretical) response of the modulator (shown in pink blow).

The next section of the code computes the expected and simulated signal-to-quantization-noise ( SQNR), which is a figure of merit when analyzing analog-to-digital converters.

The remainder of this demo essentially repeats the same process, but for a band-pass signal. That is, instead of using a low frequency sinusoid as the input, it uses a carrier signal, which makes our output spectrum look as in Figure 5 (the carrier here is at Fs/8 or 0.125 on the normalized frequency axis).

I encourage you to download the toolbox and run through the demos. Even if you do not use the toolbox for any simulations of your own, these demos can serve as a great learning tool (along with freely available online resources mentioned above).

Other MathWorks resources:

If you are interested in delta-sigma modulators (and other mixed-signal components), consider downloading the Mixed-Signal Library for Simulink. Also, take a look at the MathWorks Mixed-Signal page, particularly the ADC section.

Delta-Sigma Toolbox Usage notes:

• A few C files are provided in the Toolbox that the user needs to compile to MEX (e.g., simulateDSM.c). This is generally done as:
mex simulateDSM.c


But as noted in the comments on File Exchange, this fails on some systems. Simply defining the _STDC_ symbol should resolve the issue:

mex simulateDSM.c –D__STDC__


Note that you will need a C compiler for this step. Run mex –setup at the MATLAB command prompt to setup your C compiler.

• If you plan on using the Delta Sigma Toolbox frequently, you should probably add the toolbox folder to your MATLAB path:
addpath('<your_intall_path>\delsig')
savepath


Suggestions for improvements

• A great addition to the provided example/demo files would be the use of sections, and perhaps the publish feature to include a full write-up and description for each file.
• Providing an Install script would be great (to take care of MATLAB path and compiling MEX files).

Get the MATLAB code

Published with MATLAB® R2013a

## May 17th, 2013

Jiro's pick this week is GPUBench by Ben Tordoff.

Some of you may know of the bench function. It allows you to benchmark your MATLAB on your machine and compare it against other machines. It performs some mathematical computation tests, as well as graphics tests. Note, as mentioned in the documentation, that bench is for comparing a particular version of MATLAB on different machines, not comparing different versions of MATLAB on a single machine.

GPUBench is GPU version of bench. With Parallel Computing Toolbox, you can perform MATLAB computations on NVIDIA CUDA GPUs with Compute Capability of 1.3 or greater. But your mileage may vary depending on the hardware you have. Sometimes, looking at just the Compute Capability may not be enough to see if one card is better than another for MATLAB computations. GPUBench will test your card and compare with other common GPU cards. Here's a sample table that gets generated from the benchmark. It reports in gigaFLOPS, so higher the number, the better the performance.

When I bought a MacBookPro last year and noticed that it had a supported GPU card, I ran the benchmark on it and got the following result. Note that this was done with an older version, so the list of cards is a bit old.

The "Host PC" refers to the CPU on my Mac, and "GeForce GT 650M" is the GPU that's equipped. We can see that the GPU seems to outperform the CPU on single-precision computations but not so much on double-precision computations.

Ben periodically updates the data files to include the newer GPU models that have come out. As of the writing of this post, the version includes benchmark data for K20.