Matlab将模块化显示加载到GUI中

时间:2016-06-01 20:58:09

标签: matlab user-interface

我正在为我的机器人团队构建模块化测试模块。到目前为止,该模块看起来像这样:

Testing module

是否可以独立于此测试模块设计显示器,然后将它们加载到GUI屏幕的下半部分?

1 个答案:

答案 0 :(得分:1)

完全可以独立设计GUI“模块”。您可能必须保持对图形对象句柄的良好跟踪,但无论如何都适用于任何GUI。

我将提出一个示例,展示一种方法(尽管有其他几种方法)。这种方式需要以编程方式设计您的GUI(您可以将其调整为GUIDE设计的GUI,但这需要大量的摆弄,我不推荐它。)

基本方法是具有布置“模块”的功能(在通用容器中创建和定位所有uicontrolaxes等...)而不是figure。当您需要加载模块时,您只需创建一个容器(我在示例中使用uipanel),然后在该容器中加载模块。当您想要更改活动模块时,您只需删除容器(Matlab将删除所有子对象),然后在新容器中加载新模块。或者,您可以加载所有模块,将它们全部隐藏(在所有容器上设置'visible','off',然后只显示当前正在使用的模块。

在该示例中,定义模块的3个不同函数与main函数位于同一文件中,但它们可以分别位于单独的文件中。 (我个人使用的应用程序有近40个模块,因此您可以猜测将它们放在不同的文件中更易于管理。)

以下是它的样子,代码如下: Modular GUI

Modular_GUI_sample.m的代码: (最有趣的部分是函数Module_Selection_callback

function h = Modular_GUI_sample

%// The main figure
h.figMain = figure('Name','Testing Module',...
    'Menubar','none','Toolbar','none',...
    'NumberTitle','off','Units','Normalized' ) ;

%// The Module Selection panel
h.ms = Create_Module_Selection_UI(h.figMain) ;

%// save the ui object handles structure
guidata( h.figMain , h )


function Module_Selection_callback(hobj, ~)
selectedModuleName = get( get( hobj , 'SelectedObject' ), 'String') ;

%// retrieve the global handle structure
h = guidata( hobj ) ;
%// Delete old module if present
if isfield(h,'hmod')
    if ishandle(h.hmod.pnl) ;
        delete(h.hmod.pnl) ;    % delete the panel and children object
        h.hmod = [] ;           % clear the structure
    end
end
%// The panel which will contain the selected module
spc = 0.05 ; % spacing value for margins
h.hmod.pnl = uipanel('Units','Normalized','Position',[spc spc (1-2*spc) (0.7-2*spc)],'BackgroundColor','w') ;

%// load new module
switch selectedModuleName
    case 'Locomotion'
        hmod = Load_Module_Locomotion(h.hmod.pnl) ;
    case 'Vision'
        hmod = Load_Module_Vision(h.hmod.pnl) ;
    case 'Localization'
        hmod = Load_Module_Localization(h.hmod.pnl) ;
end
%// save the ui object handles structure
h.mod = hmod ;
guidata( h.figMain , h )


function hmod = Load_Module_Locomotion(parent)
%// main container panel
hmod.pnl = parent ;
spc = 0.05 ;
hmod.list = uicontrol('Parent',parent,'Style','listbox','Units','Normalized','Position',[spc spc (1-2*spc) (1-2*spc)]) ;
set(hmod.list,'String',{'List Item 1','List Item 2','List Item 3'},'Value',1 ) ;


function hmod = Load_Module_Vision(parent)
%// main container panel
hmod.pnl = parent ;
hmod.ax1 = subplot(2,2,1,'Parent',parent) ;
hmod.ax1 = subplot(2,2,3,'Parent',parent) ;
hmod.ax1 = subplot(2,2,[2 4],'Parent',parent) ;


function hmod = Load_Module_Localization(parent)
%// main container panel
hmod.pnl = parent ;
spc = 0.05 ;
hmod.list = uicontrol('Parent',parent,'Style','pushbutton','String','A big push button','Units','Normalized','Position',[spc spc (1-2*spc) (1-2*spc)]) ;


function ms = Create_Module_Selection_UI(parent)
%// prepare position calculation helper
spc = 0.05 ; %// spacing value for margins
nButtons = 3 ;
btnH = (1-(nButtons+1)*spc)/nButtons ;
radioPos = @(k) [spc (1-(k*(spc+btnH))) (1-2*spc) btnH ] ;

%// The main "Module Selection" panel (actually a "uibuttongroup" object)
ms.pnl = uibuttongroup('Parent',parent,'Position',[spc 0.7 (1-2*spc) 0.3-spc],'BackgroundColor','w') ;

%// prepare properties which will be common for this group of uicontrols
radioProps = {'Parent',ms.pnl , 'Style','radiobutton' , 'Units','Normalized' } ;
%// Create three radio buttons in the button group.
k=0;
k=k+1 ; ms.radLocomotion    = uicontrol( radioProps{:} , 'Position',radioPos(k) , 'String','Locomotion' ) ;
k=k+1 ; ms.radVision        = uicontrol( radioProps{:} , 'Position',radioPos(k) , 'String','Vision' ) ;
k=k+1 ; ms.radLocalization  = uicontrol( radioProps{:} , 'Position',radioPos(k) , 'String','Localization' ) ;

%// Initialize some button group properties.
set(ms.pnl,'SelectionChangeFcn',@Module_Selection_callback);
set(ms.pnl,'SelectedObject',[]);  % No selection

如果您需要以编程方式设计广泛的GUI,我建议您考虑使用文件交换中的GUI Layout Toolbox。它非常强大,与在必须以编程方式定位(和调整大小)所有控件相比,它使Matlab中的GUI设计变得轻而易举(例如,您不需要进行示例中的所有位置计算)。