Making interfaces WITHOUT guide

Interface without a guide by Cali Rossi

Original blog illustration by Cali Rossi

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 :

h=figure;

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','Position',[0.2 0.2 0.4 0.2],'Units','normalized');

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','Position',[0.2 0.2 0.4 0.2],'Units','normalized');
set(ButtonH,'Callback',@HelloCallback);

function HelloCallback(hObject,event)
msgbox('Hello world');

Related Posts

This entry was posted in Intermediate, Making interfaces. Bookmark the permalink.

3 Responses to Making interfaces WITHOUT guide

  1. Leland Harriger says:

    Could you explain why you consider uicontrol to be superior to using Guide?

    • Jerome says:

      It all depends what you want to do. Using Guide is an easy thing to do and it definitely has its advantages but it does not teach you anything about how to programmatically create the interface.
      Many user interaction can just not be created with pure guide. For instance, you can’t adjust precisely the content of a particular interface window if you don’t know how to use uicontrol.

      On top of that, Guide becomes fairly slow as the number of elements increases, so you end up loosing a lot of time and it becomes difficult to create a nice looking interface.

      I did said “Guide can be a waste of time” but I just want my fellow programmers to be able to choose between the two and decide on their particular application.
      Using guide solely does not provide a learning curve to allow you not using it.

      A last arguments is that there are a lot of nice new interface elements that you can access only programmatically, like things you download on the file exchange, or pure java objects.

  2. person says:

    great introduction ! :D

Leave a Reply