问题
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:
modify sortData-> make it a function by adding this as the first line:
function merge = sortData(..)
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