在Matlab GUIDE中绘制到指定的轴上

时间:2013-06-10 10:13:08

标签: matlab user-interface matlab-figure matlab-guide

我有一个使用Matlab GUIDE按钮的代码。它应该绘制一个旋转箭头,旋转到θ1和theta 2指定的角度。

function start_pushbutton_callback_Callback(hObject, eventdata, handles)
% hObject    handle to start_pushbutton_callback (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
theta1 = handles.xy_angle; 
theta2 = handles.yz_angle; 
delay = handles.speed;
axes(handles.axes_turntable_xy); 
R= 1; %Radius of the turntable
E=[0;0;0];
C=[-2;0;0];% points on the X axis
D=[2;0;0]; % points on the X axis
G=[0;2;0]; % points on the Y axis
H=[0;-2;0];% points on the Y axis

for th =1:1:theta1
clf;
Radius =1;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal
L1= [R*cosd(th);R*sind(th);0];
drawvector(E,L1); % call the drawvector function, as previously
line([C(1),D(1)],[C(2),D(2)]);
line([G(1),H(1)],[G(2),H(2)]);
axis([-2 2 -2 2]);
grid on;
pause(delay);
end;

axes(handles.axes_turntable_yz) ;
R= 1; %Radius of the turntable
E=[0;0;0];
C=[-2;0;0];% points on the X axis
D=[2;0;0]; % points on the X axis
G=[0;2;0]; % points on the Y axis
H=[0;-2;0];% points on the Y axis

for th =1:1:theta2;
clf;
Radius = 1;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal
L1= [R*cosd(th);R*sind(th);0];
drawvector(E,L1); % call the drawvector function
line([C(1),D(1)],[C(2),D(2)]);
line([G(1),H(1)],[G(2),H(2)]);
axis([-2 2 -2 2]);
grid on;
pause(delay);
end

但是,我在使用此代码时遇到两个问题: 1)它只是绘制到一个新的matlab图上,而不是在axes_turntable_xy上绘制它 -

2)执行直到线轴(handles.axes_turntable_yz)时显示以下错误。错误如下:

Error using axes
Invalid object handle

Error in turntable_interface_model>start_pushbutton_callback_Callback (line 235)
axes(handles.axes_turntable_yz) ;

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in turntable_interface_model (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)turntable_interface_model('start_pushbutton_callback_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

好的,我调试了代码,这是上面代码中有问题的一行:

for th =1:1:theta1
clf;
Radius =1;
[x,y,z] = cylinder(Radius,200);

工作代码是:

 for th1 =1:1:theta1
 cla(handles.axes_turntable_xy); %clears the figure handles.axes_turntable_xy
 Radius =1;
 [x,y,z] = cylinder(Radius,200);

这清除了我在Matlab中绘制的数字,而不是一些未指明的数字!尽管仍然很难理解为什么Matlab正在将这些命令绘制在一个新的图形中,尽管使当前的轴成为axis.turntable_xy。