尝试创建一个包含多功能单选按钮的MatlabGUI时遇到了问题
我在Matlab中将两个单选按钮与弹出按钮链接时遇到问题。创建这两个单选按钮是为了“调用”弹出按钮以更改两个不同绘图(选定绘图,esp或pdf)的颜色。到目前为止,我已经尝试了很多东西,但都不管用。我希望你能帮我找到解决办法 先谢谢你尝试创建一个包含多功能单选按钮的MatlabGUI时遇到了问题,matlab,button,user-interface,Matlab,Button,User Interface,我在Matlab中将两个单选按钮与弹出按钮链接时遇到问题。创建这两个单选按钮是为了“调用”弹出按钮以更改两个不同绘图(选定绘图,esp或pdf)的颜色。到目前为止,我已经尝试了很多东西,但都不管用。我希望你能帮我找到解决办法 先谢谢你 function First_Model () figure_color=[0.89 0.99 0.99]; panel_color=[1 1 1]; hFigure= figure('MenuBar','none',... 'Units','Pixels',
function First_Model ()
figure_color=[0.89 0.99 0.99];
panel_color=[1 1 1];
hFigure= figure('MenuBar','none',...
'Units','Pixels',...
'Color',figure_color,...
'Toolbar','none',...
'Name','First model',...
'resize','off',...
'NumberTitle','off',...
'Position',[1 810 1280 683]);
movegui(hFigure,'center');
warning off MATLAB:uitabgroup:OldVersion % turn off warning
%---------------Tab group---------
hg=uitabgroup('Parent',hFigure,...
'units','pixels','position',...
[1.5 10 1277 677]);
%---------------Tabs---------
ht(1)=uitab(hg,'Title','Plot');
%---------------Main Panel---------
hPanelB = uipanel(...
'Title','',...
'Fontsize',22,...
'Parent', ht(1),...
'Units','Pixels',...
'BorderWidth',1,...
'Position',[1 1 1263 640],...
'BackgroundColor',panel_color);
%---------------Sub_Panels---------
hSubPanel1 = uipanel('Parent',ht(1),'Units',...
'pixels','Title','Press Run','Fontsize',17,...
'BackgroundColor',[1 1 1],'Position',[5.5 194 595 133],...
'BorderWidth',2);
hSubPanel2 = uipanel('Parent',hPanelB,'Units',...
'pixels','Title','Plots','Fontsize',14,...
'BackgroundColor',[1 1 1],'Position',[600 3 640 615],...
'BorderWidth',2);
hSubPanel3 = uipanel('Parent',hSubPanel2,'Units',...
'pixels','Title','Adjustment','Fontsize',14,...
'BackgroundColor',[1 1 1 ],'Position',[210 230 218 135],...
'BorderWidth',1);
%------------- Plots ------
hGraph1 = axes('Parent',hSubPanel2,'Units',...
'pixels','Position',[55 420 195 135],'Color',[1 1 1]);
hGraph2 = axes('Parent',hSubPanel2,'Units',...
'pixels','Position',[415 420 195 135],'Color',[1 1 1]);
%--------Radio Buttons -----------------------------------------------------------------
hRadio = uibuttongroup('visible','off','Position',[.05,.05,.9,.9],...
'Parent',hSubPanel3,'Units','Normalized');
radio1 = uicontrol('Style','RadioButton','String',...
'F(t)','FontSize',10,'Position',[0.05,0.70,0.46,0.22],...
'Parent',hRadio,'Tag','esp','Units','normalized',...
'HandleVisibility','off');
radio2 = uicontrol('Style','RadioButton','String','P(t)',...
'FontSize',10,'Position',[0.58,0.70,0.46,0.22],...
'Parent',hRadio,'Tag','pdf','Units','normalized',...
'HandleVisibility','off');
%--- Tuner frame-------------------
hcolPopup = uicontrol('Parent',hSubPanel3,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',15,...
'ForegroundColor',[0 0 0],'Position',[0.08 0.33 0.48 0.12],...
'String',{ 'Color'; 'Red'; 'Yellow'; 'Green'; 'Blue'; 'Magenta'; 'Black'; },...
'Style','popupmenu','Value',1,'Callback',@colPopup_Callback);
%----- Push Buttons---
hMainPushButton = uicontrol('Parent',hSubPanel1,'Units',...
'normalized','Position',[0.32 0.42 0.32 0.38],'Style','Pushbutton',....
'String',' Run!','FontSize',25,'Callback',@MainPushButton_Callback);
handle_list=...
[hFigure,hg,ht(1),hSubPanel1,...
hSubPanel2,hSubPanel3,hGraph1,...
hGraph2,hRadio,radio1,radio2,hcolPopup,hMainPushButton];
set(handle_list,'Units','normalized');
set(hRadio,'Visible','on');
%--- EVALUATION FUNCTION BEGINS -------------------------------
function MainPushButton_Callback(hObject, eventdata)
x = 1:80;
o = 1-poisscdf(x,65);
p = 1-poisspdf(x,2);
axes(hGraph1);
plot(x,o,'Tag','esp');
xlabel('Xlabel','FontName','Helvetica','FontSize', 18,'fontweight','bold')
ylabel('F(t)','FontName','Helvetica','FontSize',18);grid on;set(gca,'FontSize',18)
axes(hGraph2);
plot(x,p,'Tag','pdf');xlabel('Number','FontName','Helvetica','FontSize', 18)
ylabel('P(t)','FontName','Helvetica','FontSize',18);grid on;set(gca,'FontSize',18)
end
function colPopup_Callback(hObject, eventdata)
colors=get(hcolPopup,'string');
index=get(hcolPopup,'value');
color=colors{index};
switch color
case 'Red'
h=findobj(hGraph1,'Tag','esp');set(h,'color','r')
case 'Blue'
h=findobj(hGraph1,'Tag','esp');set(h,'color','b')
case 'Yellow'
h=findobj(hGraph1,'Tag','esp');set(h,'color','y')
case 'Green'
h=findobj(hGraph1,'Tag','pdf');set(h,'color','g')
case 'Magenta'
h=findobj(hGraph1,'Tag','pdf');set(h,'color','m')
case 'Black'
h=findobj(hGraph1,'Tag','pdf');set(h,'color','k')
end
end
结束通常,您可以在单选按钮/buttongroup上进行回调,以更新某些共享状态变量,但假设允许任一绘图为六种颜色中的任何一种,则只需在关键点检查选择了哪个按钮就更简单了。这里有一种方法:
function colPopup_Callback(hObject, eventdata)
colors = get(hcolPopup, 'String');
index = get(hcolPopup, 'Value');
color = colors{index};
% since the radio buttons have the appropriate Tags set already
tag = get(get(hRadio, 'SelectedObject'), 'Tag');
h = findobj([hGraph1 hGraph2], 'Tag', tag);
switch color
case 'Red'
set(h, 'Color', 'r');
case 'Blue'
set(h, 'Color', 'b');
case 'Yellow'
set(h, 'Color', 'y');
case 'Green'
set(h, 'Color', 'g');
case 'Magenta'
set(h, 'Color', 'm');
case 'Black'
set(h, 'Color', 'k');
end
end
但是,如果我误解了这一点,并且您希望根据选择的绘图更改可选择的颜色,则需要使用回调方法相应地更新弹出菜单:
function hRadio_SelectionChangeFcn(hObject,eventdata)
switch get(eventdata.NewValue,'Tag')
case 'esp'
choices= {'Color'; 'Red'; 'Blue'; 'Yellow'};
case 'pdf'
choices= {'Color'; 'Green'; 'Magenta'; 'Black'};
end
set(hcolPopup, 'Value', 1, 'String', choices);
end
原始的
colPopup\u回调
应该足够了(尽管使用了pdf绘图的hGraph2
)亲爱的@Notlikethat,您的答案的第一部分解决了我的问题。它确实工作得很好!!真的非常感谢你!。赫克托