Saturday, March 10, 2012

GUI in MATLAB 7

I've been super busy with things, and haven't posted anything new for some time.
I'm back now.

Today's portion is about saving data (whatever type it is) in a hidden storage of a figure.
Why do we need this? Well, let's think about a hypothetical situation where you're dealing with a data of some kind.

You make a uicontrol button that opens up and load data from an excel file. This is done by a local function.
Then, you need to send it to the main function, so the main function can play with the data some place else.
The main function can send it to another local function when you press another function, and this local function will plot it on an axes or filter it out using whatever tools it has.

Of course you can think of sending your data out and returning it back to main function from the local function. However, this can easily make your code horribly complicated (well, at least that's what I experienced).

So, what if we save all the common data in one place, and different local functions can go grab it whenever they need it? There's no need to pass data from one location to another in the function, because the "storage space" is there holding it.
Ah~.

That's what we're doing this time and next.
I tried to put it in one posting, but I figured it's better to split into two.

So, let's start.

Before using the storage, you need to know where it is.
The answer is: anything you see on the screen. This storage is hidden, so you won't see it unless you go dig it out.
Anything you see on the screen... well, almost anything.
That includes your monitor screen, the figure window you just opened, the axes you just placed on that figure, the plot you just drew. They all have their own storage spaces.

Then how do you get in there and put things in it?
The magic key word is "setappdata" and "getappdata".
As you guessed it, "setappdata" put things(matrix, array, strings, etc) in a specific place.

Let's try something simple now.
When it comes to MATLAB, object handle "0" is reserved for the entire monitor space.

Let's make a test matrix, x.

Now save it to the monitor screen's storage.

Go back to the storage and bring it out.

That's not bad.
I'll show you how to use this in the GUI programming next time.

No comments:

Post a Comment