- 积分
- 1
- 注册时间
- 2004-6-25
- 仿真币
-
- 最后登录
- 1970-1-1
|
%用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}); |
|