Making interfaces WITHOUT guide

Interface without a guide by Cali Rossi

Original blog illustration by Benedicte 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 :


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 :

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

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
ButtonH=uicontrol('Parent',h,'Style','pushbutton','String','First button','Units','normalized','Position',[0.2 0.2 0.4 0.2]);

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



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

28 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 ! 😀

  3. René says:

    Essential Topic for Matlab-programmers! But, the example doesnt work in my R2013b? Could you post some Lines that will work instantly after copy and paste? I am fairly new with GUI in Matlab…
    Greetings from Germany!

    • Jerome says:

      Why do you say it doesn’t work?

      It should if you copy this code into a script file and save the file in FirstInterface.m then launch FirstInterface. It works here in R2013b

      • René says:

        Yes, now it works! Thank you!
        I dont got that is a function, not a script. Renameing the Script helped.

  4. gamersdz says:

    Thanks for the useful post and keeping it simple.
    How do i get rid of the menu?

  5. Guy says:

    Thanks for this blog. It really motivated me to leave GUIDE. I’m really struggling with something: is there a way to automatically run a function after the GUI is created? Like the “opening function” of a GUIDE-built GUI? If I just call the function at the end of the script, the GUI isn’t built yet and when the function plots, a new figure is created. 

    • Jerome says:

      You should probably call a drawnow at some point in the code to force the java thread (that gets filled when you send GUI components creations) to be updated.

      • GR says:

        Thanks, I will try that though I think my problem was I used ‘HandleVisibility’,’callback’ which prevented the script from accessing the figure.
        Thank you for your reply, and for this blog.

      • Guy says:

        Oops, used my wife’s email by mistake, so GR is actually me…

  6. George says:

    I have created a GUI without using GUIDE that works fine. But i was wondering what is the equivalent of guiname_OpeningFcn() and where it should be place in a manually created GUI ?

  7. Georgios says:

    could you be more specific on how to do it?

  8. Aditya says:

    Is it possible to write this piece of code without making the function FirstInterface ?

  9. kosha dholakia says:

    How do we pass variables between callback functions?

    For example, I have created a button (button1). The callback function of this button creates another button(button2). Now, i am storing all of the handles in a handle structure, but the handle for button2 does not show up, even after its creation

  10. Phamquyet says:

    Thank you a lot Jerome. Your tutorial is very helpful and easy to understand. thank

  11. Greg says:

    Hi, the button does not seem to be appearing with this code?
    The figure appears and I have tried changing the location but still doesnt work. Do you have any suggestions?

    Thanks for all your tutorials, they have been a great help.

  12. Greg says:

    Yes this worked perfect! Thanks for your help once again and please keep up the great work!

  13. Pingback: MatLab Basic 2 | New ThinKing

  14. Godknows says:

    Thank you very much, sir, for this excellent information!!!!!!!!!!!!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.