在 matlab GUI 上绘制嵌套函数

Plotting in a nested function on matlab GUI

我很难在我的 GUI 上使用轴,因为它说它没有定义。以下是代码摘要:

function bitirme_OpeningFcn(hObject, eventdata, handles, varargin)
axes(handles.axes1)
imshow('photo1.jpg');
...

function pushbutton1_Callback(hObject, eventdata, handles)
theta=inverseKinematic(...)
...

function [Theta1,Theta2]=inverseKinematic(angle1,angle2,angle3,desCorX,desCorY)
axes(handles.axes1);
....
plot(a,b);
....

直到函数 inverseKinematic 被调用,一切正常。然而,当它被调用时,图像不会变成图形,我在 matlab 命令 window 上看到错误“Undefined variable "handles" or class "handles.axes3"”。为什么我不能在嵌套函数中调用 axes(handles.axes1)?背后的原因是什么?

每个函数都有自己的工作区,GUIDE 生成的回调共享与所谓的 handles structure 关联的数据。 GUI 的每个组件(轴、按钮等)都与它们所在的图形相关联。因此,GUIDE 已经为回调函数提供了 handles 结构中存储的所有内容。

但是,嵌套函数无法隐式访问句柄结构中存储的数据。您需要专门向函数提供这些数据。有几个选项可用,例如:

1) 提供句柄结构作为函数的输入参数。

2) 使用 getappdatasetappdata 函数将数据与图形或 MATLAB 的根相关联

3) 在嵌套函数的开头检索句柄结构 "by hand",例如:

function [Theta1,Theta2]=inverseKinematic(angle1,angle2,angle3,desCorX,desCorY)

handles = guidata(gcf)
axes(handles.axes1);
....
plot(a,b);
....

 %// Save the changes made to the handles structure.
guidata(handles to the figure,handles);

其中gcf指的是当前数字。其实可以用图名或者hObject.

注意使用命令plot时,不需要在每次调用前设置当前坐标轴。您可以在调用中使用 Parent 属性 绘制到 select 绘制数据的位置。

示例:

plot(a,b,'Parent',handles.axes1)

我希望已经足够清楚了!