MATLAB GUI Tutorial - UITABLE Part 1, How to Display Data

Matlab Logo With the release of MATLAB 2008b, you are now able to add tables to a GUI. In the past, there was no easy way to display your data in tabular form. With the UITABLE component, displaying your data in tabular form is easy, and most importantly, looks great!

MATLAB UITABLE

Contents

Adding a Table to Your GUI using GUIDE

Within the GUIDE framework, you can add a table to your GUI using the following icon from the toolbar: MATLAB UITABLE icon.

Here’s what the GUI will look like within GUIDE:

MATLAB UITABLE tutorial

Displaying Data on the Table

We are going to populate the UITABLE component with data by pushing the “Populate Table” button. Thus, we’re going to need to add some code to the pushbutton’s callback. In the populate_pushbutton callback, we use the following code:

% --- Executes on button press in populate_pushbutton.
function populate_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to populate_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%first, create a data matrix that has 5 columns, 4 rows
myData = rand(5,4);

%now populate the table with the above values
set(handles.uitable1,'data',myData );

Now, let’s run the GUI and push the button!

MATLAB UITABLE tutorial

A neat feature is that the table is smart enough to fill out the table according to the size of the data matrix that you feed it. So if I had done the following instead:

myData = rand(100,100);

The table would incorporate the use of scroll bars, as shown below.

MATLAB UITABLE tutorial

Adding Column and Row Labels

A good way to spruce up your table is to add row and column labels. This helps differentiate your data and makes it easy to identify. Within the GUIDE framework, we can modify the labels by first bringing up the Property Inspector for the UITABLE. This can be done by double clicking the UITABLE component.

Property Insepctor

Now, if you click on any of the fields in the above picture, it will bring up the Table Property Editor. This is where you can add Row and Column labels. For example:

Edit Row Labels

Make sure you click on the “Rows”, and that you select the “Show names entered below as the row headers” option. Finally, you just need to modify the names. Similarly, you can do the same for the columns.

MATLAB UITABLE tutorial

Once you’re done with that. you should see the following:

MATLAB UITABLE tutorial

And once you run your GUI, you can see the final result. A well labeled table that displays your data beautifully!

MATLAB UITABLE

Modifying your Table through the m-file

Sometimes it’s easier to work from within the m-file framework, rather than the GUIDE framework. We could have done exactly what we did above programmatically through the m-file. In the opening function we could have done the following:

%store the row headers into a cell array
rowHeaders = {'Blink','Dagger','Loves','MATLAB','!!!!!!'};

%set the row labels
set(handles.uitable1,'RowName',rowHeaders);

%do the same for the column headers
columnHeaders = {'Quan','Daniel','Rob','Zane'};
set(handles.uitable1,'ColumnName',columnHeaders);

In this example, we assumed that we knew the dimensions of our table. If you don’t know the size of your table beforehand, then it can be difficult to apply data labels that are meaningful. By working through the m-file, you obtain more flexibility since you won’t have to go back and modify the .fig file every time you want to make a change. And if you are going to apply dynamic labeling, then working from the m-file is going to be much easier.

Next Time

Next time, we’re going to talk about how to work with manipulating the data within the table.

Links and Downloads

Download Source Files
The MathWorks Documentation for UITABLE
Cool Things You can do with UITABLE
Doug’s Video on UITABLE

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

Monday Math Madness #33: Prime Factorization

mmm
We would like to announce that the next iteration of Monday Math Madness will be the final contest that will be hosted at Blinkdagger. We might potentially continue the tradition, and focus more on MATLAB type problems, and calling the event Monday MATLAB Madness. But for now, we would like to thank everyone who has particpated in these contests and to continue following Monday Math Madness over at wildaboutmath.com

But in the mean time, here’s the latest and the greatest in the Monday Math Madness offering:

What’s the prime factorization of the smallest whole number that is divisible by all integers from 1 up to and including 50?

Here are the rules for the contest:

1. Email your answers with solutions to mondaymathmadness at gmail dot com.
2. Only one entry per person.
3. Each person may only win one prize per 12 month period. But, do submit your solutions even if you are not eligible.
4. Your answer must be explained. You must show your work! Wild About Math! and Blinkdagger will be the final judges on whether an answer was properly explained or not.
5. The deadline to submit answers is Tuesday, June 2, 12:01AM, Pacific Time. (That’s Tuesday morning, not Tuesday night.) Do a Google search for “time California” to know what the current Pacific Time is.)
6. The winner will be chosen randomly from all timely well-explained and correct submissions, using a random number generator.
7. The winner will be announced Friday, June 5, 2009.
8. The winner (or winners) will receive a Rubik’s Revolution or a $10 gift certificate to Amazon.com or $10 USD via PayPal. For those of you who don’t want a prize I’ll donate $10 to your favorite charity.
9. Comments for this post should only be used to clarify the problem. Please do not discuss ANY potential solutions.
10. I may post names and website/blog links for people submitting timely correct well-explained solutions. I’m more likely to post your name if your solution is unique.

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MMM #32 Winner, Bhaskar Bhattacharya!!!

MMM #32 Winner

mmmwin.jpg

The winner for this edition of MMM is Bhaskar Bhattacharya. Although the problem seemed quite simple, it was in fact a pretty complex problem! As you can already probably guess, the answer was NOT that the two plots of land are equal. You can view the problem statement here.

The Answer by Mark Eichenlaub

Land 2, with long N-S fences and short E-W fences, is larger. I’ll try to give intuitive and computational justification.

For most places on Earth the described plots are impossible. If the two E-W fences are different distances from the equator, they will subtend different azimuthal angles. But because they are connected by perfect N-S fences, they must have their endpoints on the same meridians, and so must subtend the same azimuthal angles. They can’t be different distances from the equator, so they must be the same distance. This means the equator runs dead center through the plot.

The qualitative question is then between wide, short plots and tall, thin ones. Imagine a wide, short plot (long E-W fences and short N-S fences). Exaggerate the situation, imagining is wraps a quarter around the Earth. If we grab the N-S fences and pull them even further apart still, we make the enclosure bigger because those N-S fences sweep out additional area as we pull them. The E-W fences are basically unaffected, though. They get longer, but only wind up running a little further along a line of latitude.

On the other hand, imagine a tall, thin plot (long N-S fences and short E-W fences). Exaggerate again, imagining the plot wraps a quarter around the globe. As we grab the short E-W and pull them further apart, they sweep out area as they’re pulled, exactly as before. But there’s a new effect. They also subtend a larger azimuthal angle as they go higher. In order to run perfectly N-S, the long fences develop a wider and wider bulge at the equator. In this case the N-S fences also sweep out new area. By the time we draw the E-W fences out near the poles, the plot covers most of the Earth. Compare this to the counter situation. When we draw the short wide plot most way around the equator, it still just covers one small band that’s a tiny fraction of the globe.

This argument shows that if we start with a 10mile*10mile plot, pulling the E-W fences apart towards the poles adds more area than pulling the N-S fences around the equator, so land 2 with long N-S fences is bigger.

Now let’s calculate the area of each plot. For land 1 (short wide plot), the N-S fences are ten miles long. The zenith angle they subtend is then ten miles divided by the radius of the Earth, or 1/395 radians. Half is above the equator, and half below, so the plot goes from pi/2-1/790 to pi/2+1/790 radians in the zenith angle. In the azimuthal angle, the E-W fences extend 20 miles. The angle they subtend is their length divided by the distance around the Earth 1/790 radians above the equator. This is 20miles/[cos(1/790)*3950miles]. We can then take the azimuthal angle to go from zero to this value.

The differential element of area on the surface of a sphere is r^2*sin(theta)*d(theta)d(phi), with ‘theta’ the zenith angle and ‘phi’ the azimuthal angle. Integrating this expression over the limits of the angles found above yields the area. Doing the computation, I found an area of 158,000*tan(1/790)mi^2. This is 200.000107 mi^2, which is pretty much what you’d expect for a rectangle 10mi*20mi.

Doing it all over for the tall, thin plot is mostly the same. Now the N-S fences subtend a zenith angle of 2/395 radians, and the E-W fences an angle of 1/[cos(1/395)*395] radians. Putting in the new limits of integration, I get an area of 79000*tan(1/395)mi^2. This is 200.000427 mi^2. Land 2 is larger by 3.2*10^-4 mi^2, or about 9000 square feet. They could flood the extra space to make a nice big pond and swim to cool off from the tropical sun.

Bhaskar’s MATLAB Code

Bhaskar also provided some MATLAB code to get the area of each plot:

function calcrect()
    S = 10; L = 20;
    calcarea(S,L)
    S = 20; L = 10;
    calcarea(S,L)
 
    function calcarea(S,L)
        % S is north-south and L is east-west
        R = 3950;
        Aring = 4*pi*(R^2)*sin(S/(2*R));
        Langle = L/(R*cos(S/(2*R)));
        Arect = Aring*Langle/(2*pi)
    end
 
end

Running it produces –

>> calcrect

Arect =

2.000001068205567e+002

Arect =

2.000004272830481e+002

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MATLAB - Data Mining in MATLAB

data_mining.png

Besides the internal blogs over at The MathWorks website, there are a few hidden gems out there that don’t get as much recognition. One of these blogs is William Dwinnel’s MATLAB blog, Data Mining in MATLAB. We’ve actually been aware of Will’s site for quite some time now. In fact, he even wrote a guest post here at Blinkdagger a while back on the topic of combinatorics.

Why I Love to Data Mine

One of the things that I enjoy about Will’s blog is that he covers a wide variety of topics. One day you might see a post on Monte Carlo, and the next day you may see a post on parallel computing or conditional entropy. His simplistic style of writing makes for an easy read. Although he doesn’t update too frequently, it’s still a good blog to keep your eye on.

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MATLAB: How to Create an Animation

Matlab Logo Animation is an effective way to display the changes in your data. MATLAB is already capable of creating a variety of 2D and 3D plots, but using animation introduces another dimension to your data visualization.

In this tutorial, you will learn how to create a series of images and collectively show them as an animation (see below for an example). The two MATLAB commands you will need to know are GETFRAME and MOVIE.


Contents

Quick Overview

So how does one create an animation video in MATLAB? Since an animation is essentially a series of images in quick succession, the key to creating an animation is to create a bunch of images at different intervals of time. In the following section, we are going to create a series of images that change over a period of time. Then, we will string these images together to create an animation.

Creating the Images

Each image is treated as a frame of the movie. An image could be any MATLAB plot that you already know how to create. The majority of the work in creating a movie involves the creation of all the images. This is usually done through a FOR loop. Let’s take the following as an example:

% Create a grid of data points
[x,y] = meshgrid(-3:0.05:3,-3:0.05:3);

% Loop to create a series of images
for i = 1:20
    % Sinusoid with its phase changing
    s = sin(x + i*0.05*pi);

    % Mathematical function of PEAKS

z =  3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
   - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
   - 1/3*exp(-(x+1).^2 - y.^2);

% Multpliy the changing sinusoid with the PEAKS
z = s.*z;

% Graph my modified PEAKS
surf(z)
zlim([-10 10])
axis off

light
shading interp
% axis off
set(gcf,'Color','white')

% Capture the figure as a frame and store it in M
M(i) = getframe;
end

Showing the Animation

To show the movie you just have to use the function MOVIE to play the frames stored in M. You can specify how many times the movie gets played and how many frames per second.

% movie(M,n,fps)
% M - stored frames
% n - loops, there are additional options with this parameter
% fps - frames per second

movie(M,1,32)

You should see something similar to the following:


Next Time

In the next post we will cover more examples of animations. Additionally I will show how you can export these animations to various formats. In the mean time, see if you can create your own animations. If you would like to share your animations and be featured in the next post, please contact us through our contact form.

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

« Prev - Next »