【问题标题】:Plotting onto specified axes in Matlab GUIDE在 Matlab GUIDE 中的指定轴上绘图
【发布时间】:2013-06-10 10:13:08
【问题描述】:

我有以下代码用于使用 Matlab GUIDE 的按钮。它应该绘制一个旋转箭头,该箭头旋转到 theta 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

有人可以帮忙吗?

【问题讨论】:

  • 能贴出设置handles.axes_turntable_xy的代码吗?大概你在 gui 的另一个函数中执行此操作。
  • @grantnz:我使用的是 GUIDE,所以我不必创建任何设置handles.axes_turntable_xy 的代码。在创建 GUI 时,我使用属性检查器将句柄可见性设置为“on”,并将标签设置为“axes_turntable_xy”。
  • 作为调试的第一步,(1) 在这个函数中设置一个断点并检查你的 handles arg 的字段和值,或者 (2) 使用 disp(fieldnames(handles)) 打印有效的字段到命令窗口。
  • 另外:为什么handles = guidata(hObject);这行?您正在覆盖传入的 handles 参数。
  • @tmpearce:我得到了答案!它已在下面发布。我已经删除了这条线。谢谢!

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


【解决方案1】:

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

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 中绘制的图形,而不是一些未指定的图形!尽管尽管将 axes.turntable_xy 设为当前轴,但仍然很难理解为什么 Matlab 将这些命令绘制在一个新图形中。

【讨论】:

    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2013-09-30
    • 2015-11-03
    相关资源
    最近更新 更多