Matlab GUI单选按钮 [英] matlab gui radio buttons
问题描述
我正在尝试@Amro的建议,并且我更改了部分代码,但出现了一些错误:/
请帮助我.
我将选项"设置为:
options = {
{'r','g','b','c','m','y','k'} ;
{'x','o','.','s'} ;
{'1','2','3'} ;
{'2','3'} ;
{'1','3'} ;
{'1','2'}
};
现在,我在matlab中写道:
mainGUI(options)
我得到了这些错误:
??? Index exceeds matrix dimensions.
Error in ==> tmp>secondaryGUI at 67
if strcmp(selected{i},options{i}{j})
Error in ==> tmp>callback at 17
hOptsGUI = secondaryGUI(hFig);
??? Error while evaluating uicontrol Callback
这是代码:
function mainGUI(options)
%# current options
opts = {'r', '.', '1'};
%# create main figure, with plot and options button
hFig = figure;
callback
%# options button callback function
function callback(o,e)
%# save current options (sharing data between the two GUIs)
setappdata(hFig, 'opts',opts);
%# display options dialog and wait for it
hOptsGUI = secondaryGUI(hFig, options);
waitfor(hOptsGUI);
%# get new options, and update plot accordingly
opts = getappdata(hFig, 'opts');
opts
end
end
function hFig = secondaryGUI(hParentFig, options)
%# create figure
hFig = figure('Menubar','none', 'Resize','off', ...
'WindowStyle','modal', 'Position',[100 100 350 200]);
movegui(hFig, 'center');
%# all possible plot options
options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
num = length(options);
%# get saved settings
selected = getappdata(hParentFig, 'opts');
%# top/bottom panels
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.0 1 0.2]);
hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.2 1 0.8]);
%# buttongroups in top panel
hBtnGrp = zeros(1,num);
width = 1/num;
for i=1:num
%# create button group
hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
%# populate it with radio buttons
height = 1./numel(options{i});
for j=1:numel(options{i})
h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
'String',options{i}{j});
%# set initially selected values
if strcmp(selected{i},options{i}{j})
set(hBtnGrp(i), 'SelectedObject',h)
end
end
end
%# save button in bottom panel
uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
'Units','normalized', 'Position',[0.3 0.2 0.4 0.6], ...
'String','start', 'Callback',@callback)
%# save button callback function
function callback(o,e)
%# get selected values
hObjs = get(hBtnGrp(:), 'SelectedObject');
vals = get(cell2mat(hObjs),{'String'});
%# update settings
setappdata(hParentFig, 'opts',vals);
%# close options dialog
close(hFig)
end
end
变量'opts'可能存在问题,因为没有以下值:{'r','.','1'}?
因为我不知道为什么它超出矩阵尺寸,所以选项的长度是6.
谢谢!
如果我正确理解了这一点,则说明您正在设计一个GUI,使用户可以设置一些参数并返回所选的选项.
您可以使用 / SETAPPDATA 作为数据共享机制.>
对于布局,请使用面板将组件分组在一起,这样可以提供更灵活的GUI.
这里是一个示例应用程序来说明.想法是,我们有一个包含绘图的主图形,并提供了第二个对话框"以自定义绘图选项.
function mainGUI()
%# current options
opts = {'r', '.', '1'};
%# create main figure, with plot and options button
hFig = figure;
hLine = plot(cumsum(rand(100,1)-0.5), ...
'Color',opts{1}, 'Marker',opts{2}, 'LineWidth',str2double(opts{3}));
uicontrol('Style','pushbutton', 'String','Options...', 'Callback',@callback)
%# options button callback function
function callback(o,e)
%# save current options (sharing data between the two GUIs)
setappdata(hFig, 'opts',opts);
%# display options dialog and wait for it
hOptsGUI = secondaryGUI(hFig);
waitfor(hOptsGUI);
%# get new options, and update plot accordingly
opts = getappdata(hFig, 'opts');
set(hLine, 'Color',opts{1}, 'Marker',opts{2}, 'LineWidth',str2double(opts{3}))
end
end
function hFig = secondaryGUI(hParentFig)
%# create figure
hFig = figure('Menubar','none', 'Resize','off', ...
'WindowStyle','modal', 'Position',[100 100 350 200]);
movegui(hFig, 'center');
%# all possible plot options
options = {
{'r','g','b','c','m','y','k'} ; %# color
{'x','o','.','s'} ; %# shape
{'1','2','3'} %# width
};
options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
num = length(options);
%# get saved settings
selected = getappdata(hParentFig, 'opts');
%# top/bottom panels
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.0 1 0.2]);
hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.2 1 0.8]);
%# buttongroups in top panel
hBtnGrp = zeros(1,num);
width = 1/num;
for i=1:num
%# create button group
hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
%# populate it with radio buttons
height = 1./numel(options{i});
for j=1:numel(options{i})
h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
'String',options{i}{j});
%# set initially selected values
if strcmp(selected{i},options{i}{j})
set(hBtnGrp(i), 'SelectedObject',h)
end
end
end
%# save button in bottom panel
uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
'Units','normalized', 'Position',[0.3 0.2 0.4 0.6], ...
'String','Save & Close', 'Callback',@callback)
%# save button callback function
function callback(o,e)
%# get selected values
hObjs = get(hBtnGrp(:), 'SelectedObject');
vals = get(cell2mat(hObjs),{'String'});
%# update settings
setappdata(hParentFig, 'opts',vals);
%# close options dialog
close(hFig)
end
end
I am trying what @Amro suggested me and I changed part of the code and I got some errors :/
please help me.
I set the Options to be:
options = {
{'r','g','b','c','m','y','k'} ;
{'x','o','.','s'} ;
{'1','2','3'} ;
{'2','3'} ;
{'1','3'} ;
{'1','2'}
};
now, I wrote in matlab:
mainGUI(options)
and I got these errors:
??? Index exceeds matrix dimensions.
Error in ==> tmp>secondaryGUI at 67
if strcmp(selected{i},options{i}{j})
Error in ==> tmp>callback at 17
hOptsGUI = secondaryGUI(hFig);
??? Error while evaluating uicontrol Callback
This is the code:
function mainGUI(options)
%# current options
opts = {'r', '.', '1'};
%# create main figure, with plot and options button
hFig = figure;
callback
%# options button callback function
function callback(o,e)
%# save current options (sharing data between the two GUIs)
setappdata(hFig, 'opts',opts);
%# display options dialog and wait for it
hOptsGUI = secondaryGUI(hFig, options);
waitfor(hOptsGUI);
%# get new options, and update plot accordingly
opts = getappdata(hFig, 'opts');
opts
end
end
function hFig = secondaryGUI(hParentFig, options)
%# create figure
hFig = figure('Menubar','none', 'Resize','off', ...
'WindowStyle','modal', 'Position',[100 100 350 200]);
movegui(hFig, 'center');
%# all possible plot options
options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
num = length(options);
%# get saved settings
selected = getappdata(hParentFig, 'opts');
%# top/bottom panels
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.0 1 0.2]);
hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.2 1 0.8]);
%# buttongroups in top panel
hBtnGrp = zeros(1,num);
width = 1/num;
for i=1:num
%# create button group
hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
%# populate it with radio buttons
height = 1./numel(options{i});
for j=1:numel(options{i})
h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
'String',options{i}{j});
%# set initially selected values
if strcmp(selected{i},options{i}{j})
set(hBtnGrp(i), 'SelectedObject',h)
end
end
end
%# save button in bottom panel
uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
'Units','normalized', 'Position',[0.3 0.2 0.4 0.6], ...
'String','start', 'Callback',@callback)
%# save button callback function
function callback(o,e)
%# get selected values
hObjs = get(hBtnGrp(:), 'SelectedObject');
vals = get(cell2mat(hObjs),{'String'});
%# update settings
setappdata(hParentFig, 'opts',vals);
%# close options dialog
close(hFig)
end
end
maybe there is a problem with the variable 'opts', because there are no values of: {'r', '.', '1'} ?
cause I dont know why it exceeds matrix dimensions, the length of options is 6.
thank you!
If I understood this correctly, you are designing a GUI that enables the user to set some parameters and return the selected options.
Instead of returning a value immediately as you are doing now, you can use GETAPPDATA/SETAPPDATA as data sharing mechanism.
As for the layout, use panels to group components together, this allows for more flexible GUIs.
Here is a sample application to illustrate. The idea is that we have the main figure containing a plot, and we provide a second "dialog" to customize the plot options.
function mainGUI()
%# current options
opts = {'r', '.', '1'};
%# create main figure, with plot and options button
hFig = figure;
hLine = plot(cumsum(rand(100,1)-0.5), ...
'Color',opts{1}, 'Marker',opts{2}, 'LineWidth',str2double(opts{3}));
uicontrol('Style','pushbutton', 'String','Options...', 'Callback',@callback)
%# options button callback function
function callback(o,e)
%# save current options (sharing data between the two GUIs)
setappdata(hFig, 'opts',opts);
%# display options dialog and wait for it
hOptsGUI = secondaryGUI(hFig);
waitfor(hOptsGUI);
%# get new options, and update plot accordingly
opts = getappdata(hFig, 'opts');
set(hLine, 'Color',opts{1}, 'Marker',opts{2}, 'LineWidth',str2double(opts{3}))
end
end
function hFig = secondaryGUI(hParentFig)
%# create figure
hFig = figure('Menubar','none', 'Resize','off', ...
'WindowStyle','modal', 'Position',[100 100 350 200]);
movegui(hFig, 'center');
%# all possible plot options
options = {
{'r','g','b','c','m','y','k'} ; %# color
{'x','o','.','s'} ; %# shape
{'1','2','3'} %# width
};
options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
num = length(options);
%# get saved settings
selected = getappdata(hParentFig, 'opts');
%# top/bottom panels
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.0 1 0.2]);
hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
'Units','normalized', 'Position',[0 0.2 1 0.8]);
%# buttongroups in top panel
hBtnGrp = zeros(1,num);
width = 1/num;
for i=1:num
%# create button group
hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
%# populate it with radio buttons
height = 1./numel(options{i});
for j=1:numel(options{i})
h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
'String',options{i}{j});
%# set initially selected values
if strcmp(selected{i},options{i}{j})
set(hBtnGrp(i), 'SelectedObject',h)
end
end
end
%# save button in bottom panel
uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
'Units','normalized', 'Position',[0.3 0.2 0.4 0.6], ...
'String','Save & Close', 'Callback',@callback)
%# save button callback function
function callback(o,e)
%# get selected values
hObjs = get(hBtnGrp(:), 'SelectedObject');
vals = get(cell2mat(hObjs),{'String'});
%# update settings
setappdata(hParentFig, 'opts',vals);
%# close options dialog
close(hFig)
end
end
这篇关于Matlab GUI单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!