hi… Can you please tell me how to access GUI (Graphical User Interface) variables in command window? Actually I have declared a button in the GUI and before this button a code runs "a=2" after pressing this button, I want to increment in this variable "a" in the command window. How may I access this variable in command window. Help me, thanks.
MATLAB: GUI
gui
Related Solutions
You need to pass the variables outside of the function by making them a global variable, or by just getting them again (if they are "stored on screen", like in a text box or checkbox). For instance:
%--Save Path Browser--
function pushbutton1_Callback(~, ~, handles) savepath = uigetdir; set(handles.edit1,'String',savepath);
This gets the variable and sets it (to an edit box). If I want to get it later (which I do) I use:
%--Next Page Button Control--
function pushbutton2_Callback(~, ~, handles) savepath = get(handles.edit1,'String')
So I just get the value from the edit box, which also gets it new each time, in case the user manually edited it. For checkboxes it is similar:
%--Next Page Button Control-- function pushbutton2_Callback(~, ~, handles) oldillumina = get(handles.checkbox2,'Value')
I just get the handle directly from the checkbox in the button function (note that this is not the checkbox's function, it is a separate button which needs the value from the checkbox). Note also that getting the value from an edit box is a string, but the checkbox is an actual number (0 or 1), so you can then use that variable directly in 'if' statements to see if a checkbox is checked or not!
If you want to use global variables, be careful with them (read the matlab documentation) as ANY function can change their value without warning, so they may change unexpectedly. For the example above, instead of getting the value again, use:
%--Save Path Browser-- function pushbutton1_Callback(~, ~, handles) global savepath savepath = uigetdir; %--Next Page Button Control-- function pushbutton2_Callback(~, ~, handles) global savepath
So now it is the same savepath variable in both functions (and all other functions, but you have to declare 'savepath' as global in each function in which you want to use it).
To run a m-file from a GUI, just insert the name of the m-file in the pushbutton callback function. It works the other way around. To open a GUI from a m-file, just type the name of the GUI in the m-file.
If you want your GUI to display values calculated within the m-file, you could define variables as global in both the GUI and the m-file.
If what you want is to display/send values to the workspace, you could use
assignin
from both your GUI or you m-file.
type
help assigin
or
doc assigin
in the command window to get all the information about the assigin built-in function.
Best Answer