Sunday, February 26, 2012

GUI in MATLAB 5

Today's portion may look like a big jump from last time, but most of it is same as the last post.
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

Thursday, February 23, 2012

GUI in MATLAB 4

Today we're making plots.
To make plots we need x and y data.
We're making 2 plots, so here we go.

Then, open a blank window using "figure", and put 2 axes on it.
If you're not familiar with "axes" this may look a bit odd. You may think "wait. I thought it was axis, not axes".
Well, they are different. You can think of it this way.
The figure you just opened is like a wall. Then you put a white piece of paper so you can draw things on it. This white piece of paper is "axes" The "axes" has x and y "axis".

Another thing to notice is "Parent" and "units". I'm not very good with other programming languages, so I don't know if others have this concept, but this is what people call "Parent" in MATLAB.
I mentioned that "axes" is like a piece of paper, right? This paper should have a wall to go to. "Parent" tells MATLAB which wall to put it to. This is quite useful when you have more than one of something to work with simultaneously.

If you have 2 figures opened up, you want to put your axes on a correct figure. We'll see this again when we put plots on the axes.

I set units to be pixel. This is something I didn't specified in the earlier posting when I was playing with figure. For some reason (unfortunately I don't know why this is default) the default setting for axes position is normalized value - going from 0 to 1. So you need to change the setting to put your axes on a precise location on the figure.

Now, let's put plots on the axes we just made. We're making 2 plots and each axes will take one.
Try the following.
 
Here I used axes handle, A1 and A2, as parents to direct which plot goes where.

This is the final result.
Ha! That was easy.

Now try this.

Now your blue line is thicker.

If you're wondering what else you can change, you can always try "get(P1)" or "set(P1)".

Next time I'll show you how to make buttons for different types of controls.



Wednesday, February 22, 2012

GUI in MATLAB 3

Last time we played with a figure handle and changed location/size of the window.

Now let's see what else is there.

Let's start with the usual.
Then try additional stuff that start with "set(F1,..."

Now the toolbar and menubar are gone and the figure background changed color to yellow.

What "set" does is changing the "properties" of the object. In the above example, "set" changes the figure's (it knows which figure it's changing because you said F1 there.) color, toolbar status, and menubar status.
Color is in normalized [Red Green Blue] array. [1 1 1] means white and [0 0 0] means black. Of course you can try [0.3 0.5 0.7] for your color. Since it's normalized [1.5 1 1] won't work.

That's right. It's pretty cool.

Do you wonder what else you can change? There are two ways to check. "set" and "get".
"set(F1)" will tell you all the property names, current property values, and other choices for the property values.
"get(F1)" gives you simply the property names and current values.

That's enough of playing with figures for now.
We'll make some plots next time. After the plot is covered we can put buttons and custom menus on the figure to make it real GUI.

Tuesday, February 21, 2012

GUI in MATLAB 2

Last time I showed you how to make a figure with a figure handle.

Now let's open a figure with whatever size you want in whatever position you need.

Type like this
Then this shows up

Do you see how that 'Position' array puts your figure in a place with size you want? The first two numbers are the "position in terms of # of pixels" of the bottom left corner of the figure in respect to the bottom left corner of your monitor screen.
The next two numbers are the width and height (in pixels) of the figure. Ah~

Now type this

Now the figure moved and changed the size. This is why we're keeping track of the figure handle instead of just drawing figure. The figure handle, F1, let you make changes and adjustment whenever needed. Using the figure handle you can save important data to the hidden storage for later use.
"set(something something)" is how you change settings on the object that belongs to the handle.
Oh~! That's right.

As I mentioned in the previous post, plots can have handles as well. Like "P1=plot(x,y,'*')". Then using this plot handle you can change it's color, thickness, line shape, etc.
I'll show you this in later posts.

Play with it a little more until you get used to the idea of this 'Position'.
Later when we put buttons and text boxes we'll use 'Position' again.

I'll show you how to change background colors and get rid of menus next time.

Monday, February 20, 2012

GUI in MATLAB 1

I've been busy with my other stuff, so there's not much to talk about C or C++.

So, I'm writing a quick start guide for GUI (graphic user interface) in MATLAB.

I'm assuming you know some about MATALB already. I hope you already know how to generate random x and y and plot them.

Ok, so GUI. Good thing about GUI is that you don't have to teach or learn how to use a program. Sometimes you write a code and a few months later you forget how to use it. That happens to many people. GUI removes that problem from the scene a lot.

Let's start with figure and figure-handle.

If you type
A figure shows up

That was easy.
Now, if you type
Same thing happens
But now you have "F1=1".
This "F1" is called figure handle and it lets you a lot more than just opening a window.
Even if you open a new figure without assigning a figure handle it still generates that number, but since it's not stored in anything it gets disregarded (well, almost).

Now type get(F1)
A whole bunch of information show up.
These are the summary of the specifications describing the figure you just opened.
"get(figure handle)" show you all about that figure. If you want to change something, you can try "set(figure handle ...)"
I'll talk more about "get" and "set" in the upcoming posts.

Many other things can have a handle. If you type "P1=plot(x,y)" when you plot something, this will let you control and manipulate that specific plot separate from other plots.

I'll write what you can do with these handles next time.

Sunday, February 19, 2012

C and Visual Studio 2010

It looks like MS Visual Studio (VS) is really well designed for C++.
Visual Studio is simply a compilation of many "Visual ..." programs. Visual Basic, Visual C++, Visual...whatever.

Check out the wikipedia page for more information regarding what else the VS has.

Since it's used more for C++ than C, you need to change a few things to use it with C.

I use VS2010, so the following pictures are all from the 2010 version.

The pictures are pretty much self explanatory. If pictures are too small, click them. They'll enlarge.


Start VS2010.
It's starting

This is the first thing you'll see.
 
Do as it says.

Keep on going.

You may want to choose differently later, but if you're just starting out C, do as it says.

Keep going.


Continue.
 



This is the last step. Now VS is ready for C programming.

That's it. I know it's a bit of hassle, but you'll get used to it.

There's a small problem. You need to do this every time you start a new project. If you open an existing project, the VS will know what you were doing before.

Tuesday, February 14, 2012

Why programming?


I'm a mechanical engineer who plays with FORTRAN a lot.
Yes, I said FORTRAN. Some people still use it.

I often tell people that I'm a semi-software engineer (because I work with endless lines of codes), but at one point I realized that I don't know much about any "practical" programming skills.

For example, if my friend ask me to write a simple program that does something simple on his computer, I don't know where to start. Bummer. What kind of software engineer is that?

So I decided to learn more of different programming languages. More than I need to know for my daily function.

I started with C just because that's where everyone starts. After C, it'll be C++. After C++, I'll study php, SQL, and HTML.

This blog will be a progress report for myself and guidance for those who want to try something different than ordinary.