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.
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.
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".
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.
Jiro is an application engineer at MathWorks. His job allows him to evangelize about MATLAB by traveling around the country to various universities and companies. He is a MATLAB addict, but he doesn't seem to mind being labeled as one.
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!
Brett is an application engineer who got his start with MATLAB in 1996 while writing a dissertation on the quantification of retinal blood flow. He became a huge fan of MATLAB, and hasn't written a line of C code since.
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.
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.
By
Guest Picker
We're a group of application engineers at MathWorks interested in a number of topics too many to list here. We hope to bring you exciting picks that span our various backgrounds.
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.
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:
Jiro is an application engineer at MathWorks. His job allows him to evangelize about MATLAB by traveling around the country to various universities and companies. He is a MATLAB addict, but he doesn't seem to mind being labeled as one.
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:
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! :)
Brett is an application engineer who got his start with MATLAB in 1996 while writing a dissertation on the quantification of retinal blood flow. He became a huge fan of MATLAB, and hasn't written a line of C code since.
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.
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:
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.
By
Guest Picker
We're a group of application engineers at MathWorks interested in a number of topics too many to list here. We hope to bring you exciting picks that span our various backgrounds.
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.
Brett is an application engineer who got his start with MATLAB in 1996 while writing a dissertation on the quantification of retinal blood flow. He became a huge fan of MATLAB, and hasn't written a line of C code since.
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.
Jiro is an application engineer at MathWorks. His job allows him to evangelize about MATLAB by traveling around the country to various universities and companies. He is a MATLAB addict, but he doesn't seem to mind being labeled as one.
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.
Brett is an application engineer who got his start with MATLAB in 1996 while writing a dissertation on the quantification of retinal blood flow. He became a huge fan of MATLAB, and hasn't written a line of C code since.
Recent Comments