我该如何在MATLAB中编写GUI界面?

8

我需要在MATLAB中为我的项目创建一个GUI。我到处寻找编写GUI的示例,但是没有找到很多。有哪些编写MATLAB GUI的好网站或技巧?

4个回答

11

9

2
最近我不得不编写一个简单的GUI来控制一些图形。我不知道你的任务是什么,但这里有一些基本的代码可以帮助你入门。这将创建两个图形;图1具有控件,图2具有y=x^p的绘图。您在框中输入p的值,然后按回车键进行注册和重新绘制;然后按按钮重置为默认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

GUI的基本思想是,当您操作称为“回调”函数或事件处理程序的控件时,这些函数能够通过控件句柄和设置/获取方法与控件进行交互以获取或更改其属性。要查看可用属性列表,请浏览Matlab文档网站上非常信息丰富的Handle Graphics Property Browser(http://www.mathworks.com/access/helpdesk/help/techdoc/infotool/hgprop/doc_frame.html),然后单击UI对象(或任何其他您需要的内容)。希望这可以帮助您!

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接