MATLAB: GUI function data to workspace and to file

workspace

I have a GUI which has a function which runs a few loops and calculates data into matrices. These data are then displayed in a plot.
However, I want the data for the plot to be accessible by a "Save plot data to file" button. My idea is to put the data from within the function that makes the plot to the workspace, and then the Callback of the "Save plot data to file" button can save the data to a file anytime from the workspace.
The problem is, I can't get the data from within a GUI callback function to workspace. Is there a simple solution for this? Do I need a global variable?
Any other ideas about how to solve the problem of saving the data to file would also be welcome. Thank you in advance, László

Best Answer

I could finally figure it out. You can save the data in the handles of the given GUI. So for example if your GUI is named "MyGUI" and you want to save the matrix M, then you can save data within a fuction as the following:
handles.MyMatrix = M; guidata(MyGUI,handles);
This will allow you to reach the values stored in M from any function, and then you can plot it, save it.