Storing workspace variable in GUI for use by other push buttons. MATLAB GUI

三世轮回 提交于 2019-12-08 10:19:32

问题


I have just started making a basic GUI with GUIDE, so far I have one push button that I am using to call a .m file I wrote earlier called sortData.m

When run outside of the GUI, sortData.m puts a matrix called "merge" into the workspace, but when I run sortData.m inside the GUI nothing happens.

I need "merge" to get stored somewhere so that my next push button can use it to output figures... how can this be done? Thanks

% --- 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)
sortData

回答1:


You can store the result inside your GUI for later use, i.e:

% --- 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)
handles.mydata = sortData;
guidata(hObject, handles);

Or you can retrieve the data from workspace when you need it, i.e:

my_merge = evalin('base', 'merge');
% do anything with my_merge here
% note that nothing affects matrix merge in workspace



回答2:


  1. modify sortData-> make it a function by adding this as the first line:

    function merge = sortData(..)

  2. modify the callback:

    function pushbutton1_Callback(hObject, eventdata, handles) merge = sortData handles.merge = merge guidata(hObject, handles);

later, within any callback that gets the handles-struct, you can retrieve the data with handles.merge. Check the documentation for more about data-sharing, see (for example):

  • TMW-newsreader
  • TMW: documentation guidata


来源:https://stackoverflow.com/questions/31910132/storing-workspace-variable-in-gui-for-use-by-other-push-buttons-matlab-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!