You know that feeling, I am sure. You know that you should learn this thing, that your current set of knowledge is limiting what you can do, that it will be so much easier once you have learned it. Still you are comfy in your current set of skills. Your brain is the lazy culprit. If you know what I am talking about, STOP that and STOP using the GUIDE to make interfaces in Matlab. It’s time to jump ahead and do the real thing. Trust me, I have been there. Read on and fly…
Today’s post is important in my mind. This is one of the thing that I knew I should have learned earlier but I was just lazy.
My job today is to help you to make interfaces in Matlab directly in your code without the help of the GUIDE. That thing is good to start but later on, it’s just a total waste of time.
First things first, you need an interface to create an interface… So we open a figure window :
This will appear on your screen. Please note how I ask for the handle h to be stored. Handles are fundamental to make interface without the GUIDE.
Next thing is to create the one most fundamental interface thing is : ‘A button’.
Most of the objects you can drag are generated using a function called uicontrol.
uicontrol needs a little set of parameter. The most important one are Style (the type of uicontrol object you create, Parent (which window to put that button on), String (the text on the button), Callback (the function being executed), Position (the size and position of the button on your interface) and Units (which units to use to position that thing in the figure).
Let’s create our first button :
ButtonH=uicontrol('Parent',h,'Style','pushbutton','String','First button','Units','normalized','Position',[0.2 0.2 0.4 0.2]);
If you are to run this code, you will get what you ask for. A window with a button at the Position you requested. But this is a pretty useless button : when you click on it, nothing happens…
To actually do something, you need to define a callback. I view callbacks as plugins for bigger objects. These are small piece of codes you provide that are executed at precise moments of the life of the object. There are callbacks everywhere in Matlab, uicontrol as many that you can define. In the case of the pushbutton, the most important one is just called Callback. To be honest, I don’t like that name because it implies that there is only one callback event associated with uicontrol but that’s the way it is.
So, if you want something to happen when the user click on that button, you need to define its callback :
set(ButtonH,'Callback',@HelloCallback); function HelloCallback(hObject,event) msgbox('Hello world'); end
Callbacks are a little counter-intuitive in their syntax. I will probably make a dedicated post in the future just about that. Briefly, you need to understand that several parameters are automatically passed to the callback function, whatever you do. This is to provide you with the bare minimum inputs that you are very likely to need in your callback functions.
These are hObject which is the handle to the object of the callback (here ButtonH) and something called event. Event is a moving target that stores what is happening with your object. Depending on your callback, this will be ever empty or something. You have to read the doc of each object.
Please note how I used @ to declare a function handle (a pointer to the function) and how I wrote hObject and event as inputs, EVEN IF I am not using them. The best you could do is to replace them with ~, but you need to define at least 2 inputs.
That’s it! If you understand this post, you can more or less do what the GUIDE do for you. Wasn’t a big deal…
Here is the full code :
function FirstInterface h=figure; ButtonH=uicontrol('Parent',h,'Style','pushbutton','String','First button','Units','normalized','Position',[0.2 0.2 0.4 0.2]); set(ButtonH,'Callback',@HelloCallback); function HelloCallback(hObject,event) msgbox('Hello world');