If you've been wondering what this figure handles, axes handles, etc. were for, today is the day you'll see what they can do.
For this post I wrote a short function. This website doesn't let me attach the file directly, so I copy-pasted the code at the bottom of this post.
So, here we go.
First it starts with declaring function. Today's portion won't work as a script m file. It needs to be a function, because it's housing another LOCAL function inside.
Let's call the main function GUI. What a name.
Then, the usual stuff follows.
Make x-array, 2 y-arrays, and open a figure, put axes on, make 2 plots with different color, and limit the axis.
Not so different from last time |
Ok. Now, add 2 buttons using uicontrol. Yes, that's right. "uicontrol" is the magic command making buttons (actually it's more than just a button, but we'll play with them next time). The idea is same as figures or axes.
There's position and background color. I didn't put "Parent" because we're dealing with just 1 figure. If you have more than one figure opened, you may want to specify that here.
Then there's this new thing. "callback"
"callback" decides what will happen if the button is activated. In this case it calls a local function (it's local because the function is sitting in a same file) called "LOCALButton".
When it calls the "LOCALButton" it sends out the plot handle with it (P1 and P2). We'll examine "callback" in more detail some other time.
Oh. It's uicontrol. That's the magic word. |
So, when the button is pressed, it calls LOCALButton and send out plot handle to the local function.
This is what the local function looks like.
The input is varargin, meaning variable argument input (or something like that). This is used when it's unclear what kind of input is coming in.
The varargin coming in is in cell array format. So you need to use {} to retrieve the information.
If you want to know what else is in there, add "varargin" there (can you guess the first number there?)
This is the local function LOCALButton |
So, what this local function does is changing on/off of the plot by changing its visibility.
Perhaps it was a bit too much for one post, but I had to cover it all to explain it.
If you're still confused, don't worry. I'll do the similar stuff again with different uicontrol types next time.
Which button do you wanna press? Blue or Red? |
Below is the source code. Select-Copy-Paste-Save as GUI.m