File Exchange Pick of the Week

June 1st, 2012

Use MATLAB GUIs with Simulink Models

Greg's picks this week are

Simulink-GUI Synchronization Example by Will Campbell, GUI development for SIMULINK models by Nitin Skandan, Simulink Signal Viewing using Event Listeners and a MATLAB UI by Phil Goddard.

Contents

Not Everyone Needs to See the Simulink Model

Often those who run simulations want to change a few inputs and parameters and see how the model responds without needing to understand the intricacies of the model itself. Providing a graphical user interface (GUI) to the model is a common approach to meet the desire to abstract the model’s internal structure for an end user of the simulation.

Why Three Picks?

As a field engineer, I work a great deal with a number of our customers who use Simulink, and I often get questions about how to provide a GUI for a Simulink model. So I set about searching the File Exchange for a good example, but instead I found three.

Each of the File Exchange entries approach adding a GUI to Simulink model differently. Each of the chosen File Exchange entries provides relatively simple examples along with documentation.

Method 1: Change Model Parameters with SET_PARAM API

Fundamentally the strategy that Will Campbell takes is to change properties and parameters of the various blocks in the Simulink model using the SET_PARAM application programming interface (API) for Simulink.

For example, in the callback function for the slider bar that appears in the GUI, the value of the Gain block in the model is updated using:

% Update the model's gain value
set_param([bdroot '/Gain'],'Gain',value)

The control of the model execution is performed using SET_PARAM in conjunction with the model’s simulation status.

Finally, the synchronization from the model to the GUI during model open, simulation start, etc. is handled using callback functions for the model and blocks within the Simulink diagram.

The way this example is constructed, the GUI and the Simulink model execute in an asynchronous fashion. The elements that Will includes in this example are an effort to enable handshaking between the GUI and the Simulink model in order to maintain synchronicity between GUI elements and Simulink model parameters.

Method 2: Custom MATLAB Code S-Function

Nitin Skandan approaches the GUI attachment to the model by using GUIs as Sink and Source blocks. That is, as blocks to either provide input to, or display outputs from, a Simulink model.

This means that the execution of the GUI code occurs during the process of model simulation and therefore less handshaking is required between the GUI and the model because of their inherent relationship using this method.

In order to create the sink block that displays the signal in a plot inside a figure window as shown in the example above, Nitin uses a MATLAB S-Function.

Nitin also provides some nice documentation to describe what was required to create this type of GUI via custom Simulink blocks.

Method 3: Use Simulink Event Listeners

The most sophisticated approach is to listen for events generated by the Simulink model during various phases of editing and execution.

This is the approach that Phil Goddard takes.

In some ways it is similar to the approach that Will took. But instead of using the Model and Block callback functions to maintain synchronicity between the model and the GUI, Phil creates event listeners that execute a function when a particular event occurs in the Simulink model.

Using this method allows for a very tight integration between the model and the GUI without the need to populate model callback functions. Therefore the code for the GUI can be maintained completely independently of the model, and won’t interfere with using the model for other applications such as manual simulations without the GUI or model referencing.

How do these methods compare?

In summary, how do the methods addressed in the three File Exchange entries above compare?

Block Model Callback Signal Access Difficulty
SET_PARAM_API No Yes No Medium
MATLAB S-Function Yes Maybe Yes Medium
Event Listeners No No Yes High

  • Block - requires adding a block to the model to enable the GUI
  • Model Callback - requires populating the callback properties of the model or blocks in the model
  • Signal Access - able to access signal values during simulation
  • Difficulty - relative difficulty of creating these types of GUI implementations

Comments

If you would like to leave any comments regarding this post, please click here.


Get the MATLAB code

Published with MATLAB® 7.14

May 25th, 2012

grep – Text searching utility

Jiro's pick this week is grep: a pedestrian, very fast grep utility by Us.

This week's pick is a recommendation from Yair, who himself is a prominent participant on MATLAB Central. Us has created a number of very useful functions over the years, and there's nothing pedestrian about his entries!

This week, I was in Seattle presenting at University of Washington, and during the seminar I received a question about the best way to scan through a file for a certain text pattern that denotes the beginning of data. She was scanning the file line by line using textscan to find the text of interest, and she was wondering if there was a more efficient way. The method she described seemed pretty reasonable. textscan is very efficient in scanning text files, and it's a good function for dealing with extremely large files, if you want to read them in chunks. Then I remembered Us's grep. I remembered Yair had suggested it, and that there were many positive responses to the entry. I suggested that she take a look at the function and to use it in conjunction with textscan to do the data reading afterwards.

Here's a quick example of how it works. In a folder called "data_files", I have 10 files, each of which contains experimental data from 100 tests. Each test is separated by a line indicating the test number.

   Test  1
   1.776199
   3.552398
   5.328597
     .
     .
     .
   Test  2
   7.250518
   4.510056
   5.797272
     .
     .
     .
   Test  3
     .
     .
     .

Because each file contains different number of data points, the file sizes are different.

fInfo = dir('data_files/*.txt');
s = [{fInfo.name}; num2cell([fInfo.bytes]/2^20)];
fprintf('%s: %4.1f MB\n', s{:})
ModelResults01.txt:  8.2 MB
ModelResults02.txt:  7.7 MB
ModelResults03.txt: 13.0 MB
ModelResults04.txt:  1.9 MB
ModelResults05.txt:  1.6 MB
ModelResults06.txt: 24.1 MB
ModelResults07.txt: 11.8 MB
ModelResults08.txt:  9.1 MB
ModelResults09.txt: 20.6 MB
ModelResults10.txt: 20.7 MB

Let's say that I want to extract data for Test 60. To identify the lines where Test 60 starts and ends, we can look for the texts "Test 60" and "Test 61".

tic;
[fl, p] = grep('-u -n', {'Test  60', 'Test  61'}, 'data_files/*.txt');
disp(' '); toc;
ModelResults01.txt:175704: Test  60
ModelResults01.txt:178682: Test  61
ModelResults02.txt:164907: Test  60
ModelResults02.txt:167702: Test  61
ModelResults03.txt:278423: Test  60
ModelResults03.txt:283142: Test  61
ModelResults04.txt:40417: Test  60
ModelResults04.txt:41102: Test  61
ModelResults05.txt:33337: Test  60
ModelResults05.txt:33902: Test  61
ModelResults06.txt:514069: Test  60
ModelResults06.txt:522782: Test  61
ModelResults07.txt:251578: Test  60
ModelResults07.txt:255842: Test  61
ModelResults08.txt:194584: Test  60
ModelResults08.txt:197882: Test  61
ModelResults09.txt:440201: Test  60
ModelResults09.txt:447662: Test  61
ModelResults10.txt:440850: Test  60
ModelResults10.txt:448322: Test  61
 
Elapsed time is 1.997956 seconds.

As you can see from the comments on the File Exchange entry page, the function runs extremely efficiently. The outputs from the function provide details about the search result, including line numbers. What I like most about Us's entry is the extensive HTML help he has on the function. He explains all the various options grep takes and the results structure that it returns, and he includes several examples that get you started.

Thanks Us for this great utility and Yair for the recommendation!

Comments

If you haven't used this, give it a spin and let us know what you think here or leave a comment for Us.

Please keep nominating your favorite File Exchange entries here.


Get the MATLAB code

Published with MATLAB® 7.14

May 18th, 2012

Read Medical Files

Brett's Pick this week is the Medical Data Reader, by Dirk-Jan Kroon.

When I first saw Sean de Wolski's recommendation of Dirk-Jan's suite of medical file readers for Pick-of-the-Week recognition, I kicked myself; I should have thought of that one on my own. (I'm sure I would have gotten around to it eventually, but sooner is better--thanks for the suggestion, Sean. Your swag is on the way!)

My background is in medical research, and in my role as an application engineer, I regularly have opportunities to work with a lot of our medically-oriented customers. The medical field is teeming with specialized data formats--many of which we don't have pre-packaged readers for--and I've steered users to this file numerous times since Dirk-Jan first shared it.

When I present MATLAB to prospective users, I often point out that we provide access to low-level functions like fopen and fread, and that the lack of a pre-built data reader should rarely (if ever) prevent anyone from analyzing their data in the MATLAB environment. If there's a spec available for the file, it's often not too difficult to create a custom file reader.

Easier still, though, is finding that someone else has coded the file reader for you! In this package--one of his many highly-rated File Exchange submissions--Dirk-Jan provides read capabilities for file of type: DICOM ( .dcm , .dicom ), V3D Philips Scanner ( .v3d ), GIPL Guys Image Processing Lab ( .gipl ), HDR/IMG Analyze ( .hdr ), ISI ( .isi ) , NifTi ( .nii ), RAW ( .raw , .* ), VMP BrainVoyager ( .vmp ), XIF HDllab/ATL ultrasound ( .xif ), VTK Visualization Toolkit ( .vtk ), Insight Meta-Image ( .mha, .mhd ), Micro CT ( .vff ), and PAR/REC Philips ( .par, .rec). (Note that support for reading, writing, and querying DICOM files is provided by the Image Processing Toolbox.)

Sean wrote of Dirk-Jan's file: "It’s reliably had a function for every file type I’ve had to work with. In addition it’s well written and user-friendly." Nice synopsis, Sean! And thanks for the great effort, Dirk-Jan!

Please continue to steer us to your favorite File Exchange submissions ...we'll continue to consider your recommendations for Pick-of-the-Week recognition!

As always, comments to this blog post are welcome. Or leave a comment for Dirk-Jan here.


Get the MATLAB code

Published with MATLAB® 7.14

May 11th, 2012

Multipath Fading Channel Simulation

Idin’s pick this week is "A MATLAB-based Object-Oriented Approach to Multipath Fading Channel Simulation" by Cyril Iskander.

This week’s pick is a bit different than usual. It’s not a nifty MATLAB trick or utility function. It’s not even a cool example of an application in wireless communications (my favorite industry!). It’s a white paper that outlines the theory behind one of MATLAB’s fundamental functions (well, Communications System Toolbox to be accurate). As a MathWorks applications engineer I am often asked questions about the technical details of our functions; this is one case where I have a really good answer.

The white paper represents one of the rare occasions where the curtain has been pulled back, and we get to see the thinking and theory that went into creating a fundamental piece of code. The paper was written in 2008 by one of the developers of MATLAB’s fading channel model, and it’s still quite relevant.

Multipath fading channels are present in virtually all wireless communication systems, and most of the literature in this field assumes some form of a fading channel. Simulating multipath fading channels accurately and efficiently is thus critical for anyone doing either research & development in the wireless industry. However, this is by no means trivial, and a great body of literature has also been dedicated to this simulation problem alone.

This submission does a nice job of providing the background on fading channels, surveying existing literature and techniques, and finally outlining the approach chosen for implementation of MATLAB’s multipath fading channel models (namely, filtered Gaussian noise approach). It also provides some examples of how these channel models are used in MATLAB.

You can find further examples and use cases of these channel models in the “demos” section of the Communications System Toolbox documentation. A good example is “COST 207 and GSM/EDGE Channel Models”.

I should note here that the same code base is also used for the Fading Channel block in Simulink. You can see examples of the Simulink block being used in most of the “Application-Specific Examples” section of the Communications System Toolbox demos. “commwman80216dstbc.mdl” is one such example (showing an 802.16d PHY layer simulation).

As always, your comments are welcomed and greatly appreciated.

May 4th, 2012

Plot Google Map

Jiro's pick this week is plot_google_map by Zohar Bar-Yehuda.

This pick comes from Chad's response to Brett's post. He says, "plot_google_map is not only cool, it’s also very easy to use and incredibly useful for plotting spatial data." I played around with this entry and I agree. It's very easy to use; you simply call the function, and it overlays a map on the current axes based on the latitude and longitude ranges. The part that I really like is the auto-refresh behavior, which automatically refreshes the map when I zoom into the map.

Here's the driving route from our headquarter (Natick, MA) to Boston Logan Airport. You should run this and try zooming into the map. Download the data here.

% load route data
load NatickToBOS

% plot route data
plot(Data001(:, 1), Data001(:, 2), 'r', 'LineWidth', 2);
line(Data001(1, 1), Data001(1, 2), 'Marker', 'o', ...
    'Color', 'b', 'MarkerFaceColor', 'b', 'MarkerSize', 10);
line(Data001(end, 1), Data001(end, 2), 'Marker', 's', ...
    'Color', 'b', 'MarkerFaceColor', 'b', 'MarkerSize', 10);
xlim([-71.4, -71]); axis equal off

% Google map
plot_google_map('maptype', 'roadmap');

One suggestion I have is to add the auto-refresh behavior for panning, in addition to zooming. It's very simple to do that. There's a section in the code that implements this for the zoom action:

zoomHandle = zoom;
set(zoomHandle, 'ActionPostCallback', @update_google_map);

You can do the same thing for the pan action like this:

panHandle = pan;
set(panHandle, 'ActionPostCallback', @update_google_map);

With this, if you pan the map, the graphics will update after releasing the mouse.

Thanks Zohar for the entry and Chad for the recommendation!

Comments

Give this a try and let us know what you think here or leave a comment for Zohar.


Get the MATLAB code

Published with MATLAB® 7.14

April 27th, 2012

Calculating arclengths…made easy!

Brett's Pick this week is Arclength, by John D'Errico.

First, a nod (and some MATLAB swag!) to Frank Engel, who steered us to John's awesome code! We recently asked users to nominate their favorite File Exchange contributions and Frank jumped in quickly to steer us to Arclength.

How timely! For a medical image processing seminar I recently put together, I wanted to measure the tortuosity of blood vessels. Defining tortuosity as the ratio of arclength to endpoint distance, I segmented an image of the retinal vasculature, skeletonized the image, and broke the vessels into sub-units after detecting branch points, and then sought to measure the length of the sub-units.

After working on the problem for a while, I came up with two reliable methods--after a few misfires. For my first attempt, I sought to reorient vessel segments so that there were no repeated "x-values," and to fit and calculate the length of splines. That approach was unwieldy, and yielded poor results. After playing around some more, I found a couple of reliable, robust approaches. First, I used regionprops to measure the perimeters of the segments, and divided that value by two--works like a charm, since the vessels were skeletonized. Next, I isolated vessel segments using bwlabel, and then calculated the maximum of the bwdistgeodesic (geodesic distance transform). Another success!

John's approach helped me to see where I went astray on my first misguided attempt, and made the solution easy:

img = imread('seg10.png');
%imshow(img);

Here are three approaches:

% Perimeter:
perim = regionprops(img,'Perimeter');
perim = perim.Perimeter/2

% Geodesic Distance Transform:
[r,c] = find(bwmorph(img,'endpoints'));
gdt = max(max(bwdistgeodesic(img,c(1),r(1),'quasi-euclidean')))

% John's arclength:
pts = regionprops(img,'pixellist');
pts = [pts.PixelList];
vessellength = arclength(pts(:,1),pts(:,2))
perim =
       74.527
gdt =
       74.527
vessellength =
       74.527

So it appears that all of these approaches work, and isn't it nice to have options? John's code is particularly useful even if your x-y- coordinates aren't necessarily extracted from an image. And because he breaks his path "into a series of integrals between each pair of breaks on the curve," he avoids the problems I had trying to fit a continuous spline to a rotated set of coordinates.

Very nice, John. And thanks for the note, Frank. Swag on the way to both of you!

Keep those suggestions coming. This makes my job a lot easier! :)

As always, comments to this blog post are welcome. Or leave a comment for John here.


Get the MATLAB code

Published with MATLAB® 7.14

April 20th, 2012

Run on Target Hardware

Doug's picks this week are Simulink Support Packages for LEGO MINDSTORMS NXT [link], Beagleboard [link], Arduino Uno [link], and Arduino Mega [link] by the Classroom Resources Team.

This week, I'd like to highlight a new feature in the R2012a release of Simulink called Run on Target Hardware.

You may remember back in November 2010 Greg Wolff highlighted my Arduino Target submission that let you run a Simulink model on an Arduino Deumilanove by leveraging Simulink Coder and Embedded Coder to convert the model to C code.

So what's different about this new Run on Target Hardware capability? Well starting in R2012a you can now run your Simulink models on hardware even if you do not have Simulink Coder or Embedded Coder. That means that even those of you using the Student Version can take advantage of this capability! Run on Target Hardware currently supports the Uno and Mega varieties of the Arduino as well as the BeagleBoard-xM and LEGO MINDSTORMS NXT.

While there are File Exchange submissions for the supported hardware targets, you should install them directly from MATLAB by typing "targetinstaller" at the command prompt. This brings up a tool that automates the download and install process. Watch the video below to see it in action:

Video: Introduction to Simulink Support for Target Hardware
Introduction to Simulink Support for Target Hardware 1:57

If you do have Simulink Coder and Embedded Coder, the original target is still available and is known as the Embedded Coder Support Package for Arduino. It supports the ability to do processor-in-the-loop simulations. For more information on the different ways Arduino is supported with MATLAB and Simulink see this solution page.

Comments

We can't wait to see what kind of interesting projects you come up with by taking advantage of this capability. Please check it out and let us know what you think here.

April 13th, 2012

What is your favorite unrecognized File Exchange submission?

Contents

This week, instead of Picking one of my favorite files, I'm going to solicit your suggestions for Pickworthy files. Except on rare occasions, I tend to select submissions that are useful to my own workflows. But I recognize that many of you use MATLAB and Simulink differently than I do, and in different fields. So here's your chance to recognize something that you particularly like.

You make the call!

Respond to this post with suggestions for future Picks. What have you found most useful? Perhaps you've found some overlooked gems? Something you think would have broad appeal, if they only knew about it? And if Jiro or I end up selecting a file as a Pick of the Week based on your suggestion, we'll send you some cool MATLAB swag!

A couple ground rules.

  • Please don't steer to your own file. (We may have another round of this later, in which we allow contributors to nominate their own files, but for this round, let's focus on recognizing the work of our fellow MATLAB or Simulink fans.)
  • 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.)
  • Remember: cool MATLAB swag to anyone who steers us to a file we use!
  • Please direct your correspondences to the comments section of this post.
  • Happy hunting!
  • Sorry, one more rule: we're only going to consider files that don't use undocumented functionality.


Get the MATLAB code

Published with MATLAB® 7.14

April 6th, 2012

Autostereogram

Jiro's pick this week is abSIRD by Daniel Armyr.

Here's a fun pick for the week. This function takes a matrix, representing a height-map, and visualizes it using an Autostereogram. To quote Daniel, "the algorithm used is abSIRD, published in 2004 by Lewey Geselowitz. It is a fast, in-place algorithm that is exquisitely simple to implement."

Can you see them? Hint: cross your eyes.

m = membrane(1, 250, 9, 2);
makeAbsird(m);
p = load('penny');

% Make it 4 times bigger (imresize from Image Processing Toolbox)
p2 = imresize(p.P, 4);

makeAbsird(p2);

What a great way to visualize 3D data!

I like this entry not only for the coolness factor, but also for how it is written. The code is robustly written with error checking and plenty of comments to describe the algorithm. I also like the published example code that he included with the entry. These files can be easily created using MATLAB's publishing capability, and it's a great way of showing how to use your code or explaining concepts. I especially like them when they are used with File Exchange entries.

Comments

Give this function a try (and cross your eyes) and let us know what you think here or leave a comment for Daniel.


Get the MATLAB code

Published with MATLAB® 7.14

March 30th, 2012

Archimedes’s Computation of Pi

Brett's Pick this week is The Computation of Pi by Archimedes, by Bill McKeeman.

In the comments to Bill's post, long-time File Exchange champion John D'Errico wrote: "There are two uses for the File Exchange that I love. One is to provide high quality code. The second is to teach others about some interesting part of mathematics and hopefully, write it using well written MATLAB code. This submission eminently qualifies. You can read it and learn something from what Bill has done."

I wholeheartedly agree with John about the value of Bill's post. In this submission, retired MathWorks Fellow (and current (?) Dartmouth adjunct faculty member) Bill provides a beautifully presented explanation of Archimedes's attempt to measure pi by calculating the circumferences of regularly polygons bounding the inside and outside of a circle:

Then, by successively increasing the number of sides of those polygons, Archimedes iterated to a more accurate value of pi than had been obtained to date.

Besides making a fascinating read, Bill's presentation of this material provides a really nice demonstration of using HTML to show mathematics-intensive text. Once rendered using MATLAB's built-in publishing capabilities, you end up with a beautifully formatted report.

As always, comments to this blog post are welcome. Or leave a comment for Bill here.


Get the MATLAB code

Published with MATLAB® 7.14


MathWorks

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

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