【问题标题】:Matlab GUI, share data among callback functionsMatlab GUI,在回调函数之间共享数据
【发布时间】:2014-12-01 06:10:59
【问题描述】:

我花了很长时间才做到这一点。我可以搜索很多教程来教这个,但无论如何我都做不到,所以我需要一些帮助,不得不在这里提问。在 GUI 中,有两个列表框和两个按钮。我点击 pushbutton2,我们得到数据。现在在函数 pushbutton1_callback 中,我需要使用数据。我不想使用“全球”,因为它不是一个好方法。非常感谢。

function pushbutton1_Callback(hObject, eventdata, handles)

% I need to get  data from function pushbutton2

function pushbutton2_Callback(hObject, eventdata, handles)

data = get(handles.listbox2,'String')

【问题讨论】:

  • 将数据存储在图的'UserData'属性中

标签: matlab user-interface listbox


【解决方案1】:

使用 GUI 的句柄结构来存储您想要的所有数据。看看here

function pushbutton2_Callback(hObject, eventdata, handles)

handles.data = get(handles.listbox2,'String') % Store the data directly in  the structure.

guidata(handles,hObject); %// update the structure. Important!

function pushbutton1_Callback(hObject, eventdata, handles)

%// Here use the data as you wish:

DatatoUse = handles.data;

你可以走了。

【讨论】:

  • 您好,您的回答确实解决了我的问题,但又引发了一个新问题。当涉及到 guidata(handles,hObject);句柄中的东西已更新,我仍然有这个代码:newitem = handles.element。这个 newitem 改变了,我需要 newitem 保持不变。你知道怎么处理吗?多谢。我还是 GUI 新手。您需要更多详细信息来解决它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2016-04-29
  • 1970-01-01
  • 2019-07-02
  • 2011-01-13
  • 2011-06-03
  • 1970-01-01
相关资源
最近更新 更多