找回密码
 注册
Simdroid-非首页
查看: 106|回复: 3

【求助】关于Radio Button控件应用的一个问题

[复制链接]
发表于 2005-7-5 22:11:25 | 显示全部楼层 |阅读模式 来自 辽宁大连
%用matlab gui做了一个简单的界面,一个Edit Text控件用于输入振幅a,一个Push Button控件用于计算y1=a*sin(t)
%y2=a*cos(t),一个Popup Menu控件用于选择y1或y2并在Axes控件上绘制,一个Radio Button控件:当处于选中状态时
%实现光标在曲线y1或y2上取点,当处于非选状态时在曲线y1或y2拖动鼠标实现zoom功能。主要回调函数代码如下(其中function crosshair函数取自本论坛帖子):
%现在的问题是:运行后,选择y1,选中Radio Button,不能实现光标取点,再选择y2,Radio Button仍处于选中状态不动,则
%能实现取点,总之,不能实现我说的功能。
function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a=str2double(get(handles.edit1,'String'));
t=0:0.01*pi:6*pi;
y1=a*sin(t);
y2=a*cos(t);
set(handles.pushbutton1,'UserData',[y1;y2]);

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y=get(handles.pushbutton1,'UserData');
val1=get(handles.popupmenu1,'Value');
switch val1
case 1
    plot([0:0.01*pi:6*pi],y(1,:));
case 2
    plot([0:0.01*pi:6*pi],y(2,:));
end
f=get(handles.radiobutton1,'Value');
if f==1
    crosshair;
else
    hf=gcf;
    zoom(hf,'on');
end
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function crosshair

%ezplot('sin(x)');
%zoom on;%不可同时启用
set(gcf,'WindowButtonDownFcn',@myfcn);
    flag=1;
    xlim=get(gca,'xlim');
    xlim=[xlim(2)-xlim(1)]*0.025;
    ylim=get(gca,'ylim');
    ylim=[ylim(2)-ylim(1)]*0.03;
set(gcf,'userdata',[flag,xlim,ylim]);

  %%%%%%%%%%%%%%%%%%%%%%%%%%
function myfcn(hObject, eventdata, handles);
pt=get(gca,'currentpoint');
pt_show=[pt(1,1),pt(1,2)]   
temp=get(gcf,'userdata');
if iscell(temp)
    H=temp{2};
    temp=temp{1};
end
flag=temp(1);xlim=temp(2);ylim=temp(3);
      
   if flag==1
      H.xline=line([pt(1,1)-xlim,pt(1,1)+xlim],[pt(1,2),pt(1,2)]);
      H.yline=line([pt(1,1),pt(1,1)],[pt(1,2)-ylim,pt(1,2)+ylim]);
      flag=0;
   else
      set(H.xline,'xdata',[pt(1,1)-xlim,pt(1,1)+xlim]);
      set(H.xline,'ydata',[pt(1,2),pt(1,2)]);
      set(H.yline,'xdata',[pt(1,1),pt(1,1)]);
      set(H.yline,'ydata',[pt(1,2)-ylim,pt(1,2)+ylim]);
   end
set(gcf,'userdata',{[flag,xlim,ylim],H});
 楼主| 发表于 2005-7-6 16:31:35 | 显示全部楼层 来自 辽宁大连

Re:【求助】关于Radio Button控件应用的一个问题

Simdroid开发平台
这个问题我终于解决了,用Radio Button控件产生zoom功能,而不是响应WindowButtonDownFcn动作就对了
发表于 2005-7-6 22:28:38 | 显示全部楼层 来自 新疆乌鲁木齐

Re:【求助】关于Radio Button控件应用的一个问题

请在上面你的代码中对你已经解决的问题做出调整改动,使之能够成功运行,然后请在第二贴中给予调试前后的详细说明,谢谢!
(simwe永远欢迎能独立解决问题的人,无论他(她)现在是否是高手!老兄要好好解释一下你的程序哦,因为这个是加分的前兆:D:D)
 楼主| 发表于 2005-7-7 09:02:27 | 显示全部楼层 来自 辽宁大连

Re:【求助】关于Radio Button控件应用的一个问题

%谢谢bainhom斑竹的鼓励,其实我是个新手,在这学到了不少东西,
%包括bainhom斑竹一些精彩的帖子,希望今后各位能多指点。以下为修改
%后的代码。可能有的地方不够简洁,但能实现我说的功能
%即:选中Radio Button实现zoom功能,再次点击使Radio Button处于
%非选状态,则实现光标取点(在鼠标点下的地方画十字光标,并在matlab
%命令行中显示坐标值)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
a=str2double(get(handles.edit1,'String'));
t=0:0.01*pi:6*pi;
y1=a*sin(t);
y2=a*cos(t);
set(handles.pushbutton1,'UserData',[y1;y2]);

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
f2=get(handles.radiobutton1,'Value')   
if f2==1
   zoom(gcf,'on');
else
    set(gcf,'WindowButtonDownFcn',@myfcn);
end
% Hint: get(hObject,'Value') returns toggle state of radiobutton1

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
y=get(handles.pushbutton1,'UserData');
val1=get(handles.popupmenu1,'Value');
switch val1
case 1
    plot([0:0.01*pi:6*pi],y(1,:));
case 2
    plot([0:0.01*pi:6*pi],y(2,:));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f2=get(handles.radiobutton1,'Value')
if f2==0
   set(gcf,'WindowButtonDownFcn',@myfcn);
else
   zoom(gcf,'on');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%已经说了关于WindowButtonDownFcn动作的响应函数myfcn()是在本论坛
%学习的,在此做了些改动,功能就是画十字光标。
function myfcn(hObject, eventdata, handles);
xlim=get(gca,'xlim');
Xlim=[xlim(2)-xlim(1)]*0.025;
ylim=get(gca,'ylim');
Ylim=[ylim(2)-ylim(1)]*0.03;
pt=get(gca,'currentpoint');
pt_show=[pt(1,1),pt(1,2)]           
line([pt(1,1)-Xlim,pt(1,1)+Xlim],[pt(1,2),pt(1,2)]);
line([pt(1,1),pt(1,1)],[pt(1,2)-Ylim,pt(1,2)+Ylim]);

% --- Executes on button press in radiobutton2.

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-11-1 08:34 , Processed in 0.059671 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表