How can I program a GUI in MATLAB?

15,282

Solution 1

The first place you need to go is Matlab Help on Creating Graphical User Interfaces .

Then, you can watch this tutorial video or this one

This tutorial is also good.

Solution 2

Here are all the videos that I have made about making MATLAB GUIs

http://blogs.mathworks.com/videos/category/gui-or-guide/

Solution 3

These 41 complete GUI examples posted to the MathWorks File Exchange by Matt Fig are a great place to start. The submission was even a Pick of the Week.

Solution 4

I recently had to program a simple GUI that controls some plots. I don't know exactly what your task is, but here's some basic code to get you started. This creates two figures; Figure 1 has controls, Figure 2 has a plot of y=x^p. You enter the value of p into the box and press enter to register it and replot; then press the button to reset to default p=1.

    function SampleGUI()
    x=linspace(-2,2,100);
    power=1;
    y=x.^power;
    ctrl_fh = figure; % controls figure handle
    plot_fh = figure; % plot figure handle
    plot(x,y); 
    % uicontrol handles:
    hPwr = uicontrol('Style','edit','Parent',... 
                         ctrl_fh,...
                         'Position',[45 100 100 20],...
                         'String',num2str(power),...
                         'CallBack',@pwrHandler);

    hButton = uicontrol('Style','pushbutton','Parent',ctrl_fh,...  
                        'Position',[45 150 100 20],...
                        'String','Reset','Callback',@reset); 

    function reset(source,event,handles,varargin) % boilerplate argument string
        fprintf('resetting...\n');
        power=1;
        set(hPwr,'String',num2str(power));
        y=x.^power;
        compute_and_draw_plot();
    end

    function pwrHandler(source,event,handles,varargin) 
        power=str2num(get(hPwr,'string'));
        fprintf('Setting power to %s\n',get(hPwr,'string'));
        compute_and_draw_plot();
    end

    function compute_and_draw_plot()
        y=x.^power;
        figure(plot_fh); plot(x,y);
    end
end

The basic idea behind GUIs is that when you manipulate controls they call "Callback" functions, i.e. event handlers; these functions are able to interact through controls using the control handles and set/get methods to obtain or change their properties.

To get to the list of available properties, peruse the very informative Handle Graphics Property Browser on Matlab's documentation website (http://www.mathworks.com/access/helpdesk/help/techdoc/infotool/hgprop/doc_frame.html); click on UI Objects (or whatever else that you need).

Hope this helps!

Share:
15,282
suphero
Author by

suphero

Senior Software Developer

Updated on September 16, 2022

Comments

  • suphero
    suphero over 1 year

    I need to create a GUI in MATLAB for my project. I looked everywhere for examples of how to program a GUI but I couldn't find a lot. What are some good sites or techniques for GUI programming in MATLAB?