MATLAB: Transferring variables between script and GUI - matlab

I am new to using GUIDE and using GUIs in Matlab.
I am running a script in Matlab that will accept initial inputs from the user and then proceed to open up a GUI (created with GUIDE) which will accept further inputs from the user. However, I cannot figure out a way to transfer variables and data between my script and my GUI. I am aware that script variables/data and GUI variables/data are saved in different workspaces; I just need some simple way to communicate between the two.
Thanks.

A simple way would be to use setappdata and getappdata in order to store your variables/input/whatever so that it's accessible from wherever you want.
Let's say you delcare some variable in your script that you want to retrieve in your GUI:
A = rand(100,100);
Then using setappdata like the following:
setappdata(0,'VariableName',A);
will store the data in the root directory (the 0 as the 1st input), meaning that using getappdata like this in the GUI:
A_in_GUI = getappdata(0,'VariableName');
will allow you to retrieve the value from your GUI, or from any other script as long as you use the correct variable names of course. Notice that you can use a handle to some figure/GUI where you could save your data, like this;
setappdata(handles.Something,'VariableName','A);
but if you close the figure, for instance, you might not be able to retrieve your variable.
As Tyler pointed out, a nice way to share data between different callbacks inside your GUI is to use its handles structure. More info here.
Final Note:
If you don't want to spend your time sharing many variables between scripts and GUIs, you can store all of your variables in a single large structure and use setappdata/getappdata only on this structure, which will keep all your variables updated.
For instance, you can write something like this in the script:
Variables_Structure.MyFavoriteNumber = pi;
Variables_Structure.MyFavoriteSport = 'ice hockey';
setappdata(0,'MyVariables',Variables_Structure);
and then getappdata in the GUI in which you want to use the variables:
Variables_in_GUI = getappdata(0,'MyVariables');

Related

Collect internal variables of nested functions in matlab

I have a project consisting of multiple nested functions.
For debugging purpose I want to save all internal variables in one way or another, in order to display figures, replay parts of code etc...
I also want to keep this as transparent as possible regarding calculation time.
My first thought was to create a global variable, and store programmatically at the end of each function the inputs and outputs inside the variable as a structure :
globalVariable.nameOfParentfunction_NameOfFunction.nameInput1 = valueInput1;
globalVariable.nameOfParentfunction_NameOfFunction.nameInput2 = valueInput2;
...
globalVariable.nameOfParentfunction_NameOfFunction.nameOutput1 = valueOutput1;
...
Is it possible to use some kind of reflection to get the name and value of inputs/outputs without necessarily parse the file where the function is written?
I found a good but maybe outdated topic about parsing
How do I collect internal signals?
The simple solution is to use save, which will save all variables in the current workspace (the function’s context) to file.
If you want to keep the values in memory, not in a file, you can use names = who to get a list of all variables defined in the current workspace, then use val = eval(names{i}) to get the value of the variable called name{i}.
I would recommend putting all of that in a separate function, which you can call from any other function to store its variables, to avoid repeating code. This function would use evalin('caller',…) to get names and values of variables in the workspace of the calling function.
Note that using eval or evalin prevents MATLAB from optimizing code using its JIT. They also are dangerous to use, since they can execute arbitrary code, however in this case you control what is being executed so that is no a concern.

Importing values from one window to another in MATLAB GUI

Suppose that I have a button in a window that when I click on it a new window will appear. I call this window (with the name of My_New_Window) with this syntax:
My_New_Window();
I want to insert some values to this new window from main window. I know that I can use setappdata or getappdata for this purpose but Is there another way to this? For example like this syntax:
My_New_Window(Values);
Another question. When we use setappdata or getappdata, where MATLAB stores this data? In the RAM or Hard drive?
Yes, you can use My_New_Window(Values); For example, in GUIDE, whatever parameters you pass to your GUI, you can handle in the OpeningFcn using its varargin input. Simply assign varargin to your handles structure and use guidata(hObject, handles);
Regarding setappdata - according to this book the data is stored inside an "object". Since objects reside in memory, it is safe to assume that it is indeed kept in RAM.
You can store data in the GUI UserData property:
set(handletoFigure,'UserData',Values);
when you open the other GUI you retrieve the info:
Values = get(handletoFigure,'UserData);
Is there a reason why you don't want to use setappdata/getappdata?
As for your 2nd question I don't know sorry. I guess it's the RAM though

MATLAB GUI: Exchange data (handles)

I have designed a MATLAB GUI by GUIDE for image analysis. I need to share data between functions so I used the guidata function and stored it in the handles-object as it is documented (http://www.mathworks.de/de/help/matlab/ref/guidata.html).
For the auto generated callback functions (which receive handles automatically) this works well, however I also want to modify the data in self-written functions and self-written callback functions (like click on image events). I tried manually passing the handles object which gives me read access to the data but no way to store it. I tried passing the object handle too, to use guidata(hObject, handles) but the object handle does not work then.
In short: I need a way to read&write data from all functions in the file. I'm looking for a more elegant way than making everything global. That would be my last resort.
Do you have any ideas?
In GUIs, you can use the function setappdata / getappdata in order to store and share data structure between functions (link to docs).
You can use the figure as the handle. For example:
appData = struct;
appData.image = someImage;
appData.title = "someTitle";
setappdata(handles.figure1,'data',appData);
Later, you pass handles to your functions, and you can retrieve your data:
function showTitle(handles)
appData = getappdata(handles.figure1,'data');
title = appData.title;
newTitle = "someNewTitle";
appData.title = newTitle;
setappdata(handles.figure1,'data',appData);
EDIT: Just found this link, which specifies multiple strategies to share data among callbacks.
Thank you a lot! I found the error while trying to produce a reproducebable example. In my case I was using the image handle instead of the figure handle in one function because it was an image click callback and inside that function the image was redrawn so the handle wasn't valid any more.
I use gcf now to obtain the figure handle and it works fine.

How to set global variables for different GUIs in Matlab?

I want to do something like this: a program where at the beginning some data is initialized. Then I open other GUIs where I show some graphics from these data.
Is there any possible way to set some global variables for different GUIs? If not, how could I do it?
As long as you pass the handle of the first GUI to the second, you can access all the properties of GUI#1 from GUI#2 (and vice versa, since the GUI creation method returns the handle to GUI#2).
Alternatively, you can collect the data you want to visualize (as well as the handles of the GUIs) in a handle object, which means it is passed by reference and thus available everywhere.
Finally, you can, of course, create global variables - at the beginning of any function that would like to access these variables you have to declare them as global var1, var2, var3.

Pass variable from GUI to function in MATLAB

I have a MATLAB GUI that loads to aid in visually pre-processing data. Essentially it prompts the user to adjust the data range, reduce number of data points, etc... all while providing an updated graph. Upon completion of this work, I want to be able to close out the GUI and pass variables from the GUI to another MATLAB function that does the data analysis. I have found lots of information on how to pass information from a function TO and GUI, but not the other way around.
Any help would be greatly appreciated.
Global variables can cause hard to find bugs. The best solution for your problem (where you want to pass the data directly to another function on close) might be to call the analysis function from the Figure Close Request Function. When the figure your GUI is running in is told to close, it will run the code in this function, which can call your analysis function and have access to the GUI's data.
Matlab GUIs are functions: the code exists in a .m file just like other functions. Like regular functions, they can have return values. You can get as fancy as you want messing with the varargout system, or you can simply return a value, structure, or cell array containing whatever you want. Open up the m-file and edit it to return what you want it to.
Note: If you require special processing when the figure is being closed to generate the appropriate return value, you can reimplement the closeRequestFcn as you see fit.
The easy way: you declare as global variable, where variable stores the data that you want to carry from the GUI to the main MATLAB workspace. Then, you also declare the same global variable on the command window. Hereinafter, variable to be accesible from both scopes, the GUI and the main workspace.
You could also use save or any other alternatives as csvwrite or dlmwrite to store the data into a file, but this doesn't seem to be your case.