我在 MATLAB 的 GUI 中工作,我使用选项卡来组织信息。由于 MATLAB GUIDE 不支持选项卡,我只创建了几个 uipanel
并更改它们的 'Visible'
字段。但是,当每个面板内的控件数量很大时,在面板之间切换需要一些时间。有谁知道加快标签切换速度的方法吗?
我包含一个基于选项卡的界面的简单示例。
tab_example_gui.m
% Figure
handles.figure_window = figure(...
'Units','characters',...
'Tag','figure_window',...
'Position',[50 50 80 25],...
'Name','Tab Example',...
'DockControls','off',...
'IntegerHandle','off',...
'MenuBar','none',...
'NumberTitle','off',...
'Resize','off');
% Buttons
handles.tab_panel = uibuttongroup(...
'Parent',handles.figure_window,...
'Tag','tab_panel',...
'Units','characters',...
'Position',[0 23 80 2],...
'SelectionChangeFcn',@(hObject,eventdata)tab_example_callback(hObject,eventdata,guidata(hObject)),...
'BorderType','none');
handles.tab_a = uicontrol(...
'Parent',handles.tab_panel,...
'Tag','tab_a',...
'Units','characters',...
'Position',[0 0 40 2],...
'Style','togglebutton',...
'String','Tab A');
handles.tab_b = uicontrol(...
'Parent',handles.tab_panel,...
'Tag','tab_b',...
'Units','characters',...
'Position',[40 0 40 2],...
'Style','togglebutton',...
'String','Tab B');
% Panel A
handles.panel_a = uipanel(...
'Parent',handles.figure_window,...
'Tag','panel_menu',...
'Units','characters',...
'Position',[0.1 0 79.8 23],...
'Visible','On');
handles.panel_a_text = uicontrol(...
'Parent',handles.panel_a,...
'Tag','panel_menu_load_id_text',...
'Units','characters',...
'Position',[0 0 77 22],...
'Style','text',...
'String','This is the tab A');
% Panel B
handles.panel_b = uipanel(...
'Parent',handles.figure_window,...
'Tag','panel_menu',...
'Units','characters',...
'Position',[0.1 0 79.8 23],...
'Visible','Off');
handles.panel_b_text = uicontrol(...
'Parent',handles.panel_b,...
'Tag','panel_menu_load_id_text',...
'Units','characters',...
'Position',[0 0 77 22],...
'Style','text',...
'String','This is the tab B');
guidata(handles.figure_window, handles);
tab_example_callback.m
function tab_example_callback(hObject,eventdata,handles)
switch get(get(hObject,'SelectedObject'),'Tag')
case 'tab_a', set(handles.panel_a,'Visible','On'); set(handles.panel_b,'Visible','Off');
case 'tab_b', set(handles.panel_a,'Visible','Off'); set(handles.panel_b,'Visible','On');
end
guidata(handles.figure_window, handles);
end
注意:GUI 用于在 5 个选项卡中引入模拟参数。在每个选项卡中,我有大约 15 行;每行有一个text
,一个checkbox
和三个edit
。对我来说它看起来并不拥挤。此外,我已经用最少的代码和开销自行完成了布局和回调。但是,它仍然有非常烦人的选项卡转换。
最佳答案
也许如果你把句柄放在一个数组中。这样你就不必逐一检查。这对我来说似乎很快,但“快速”可能意味着很多事情:)
我删除了一些属性以使示例更短...
function test(N) % N is the number of tabs
if nargin == 0
N = 3;
end
% Figure
handles.figure_window = figure(...
'Units','characters',...
'Position',[50 50 80 25]);
% Buttons
handles.tab_panel = uibuttongroup(...
'Parent',handles.figure_window,...
'Units','characters',...
'Position',[0 23 80 2]);
alpha = 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
for i_tab=1:N
% button
handles.tabs(i_tab) = uicontrol(...
'Parent',handles.tab_panel,...
'Units','characters',...
'Position',[80/N*(i_tab-1) 0 80/N 2],...
'Style','togglebutton',...
'String',['Tab ' alpha(i_tab)]);
% Panel i
handles.panels(i_tab) = uipanel(...
'Parent',handles.figure_window,...
'Units','characters',...
'Position',[0.1 0 79.8 23],...
'Visible','Off');
handles.panel_a_text = uicontrol(...
'Parent',handles.panels(i_tab),...
'Units','characters',...
'Position',[0 0 77 22],...
'Style','text',...
'String',['This is the tab ', alpha(i_tab)]);
end
% set callback for all buttons
set(handles.tabs, 'callback', {@tab_example_callback handles})
% choose tab 1 as active
set(handles.panels(1), 'Visible','On');
guidata(handles.figure_window, handles);
function tab_example_callback(hObject,eventdata,handles)
% set everything invisible
set(handles.panels,'Visible','Off');
% turn on selected panel
set(handles.panels(handles.tabs == hObject), 'Visible','On');
guidata(handles.figure_window, handles);
这对你有帮助吗?
关于matlab - MATLAB GUI 中的优化选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14688378/