Accessing function data in GUI
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm new to creating GUIs, and am trying to construct something that seems on the basic side. However, I have some questions...
I'm trying to access a value that is stored inside a function. For example, I have a few toggle buttons. AFTER they are all set, I want to have code that will check the state of each, and perform an action based on these states when a push button is pressed. I'm not sure how to pass the toggle state values outside of their functions.
Another example I am having an issue with is loading a file and using the filename outside of the function. When I load the file using a push button and uigetfile, the name and path should be stored. How can I access the filename string outside the function? I want to set the filename string as the name of another toggle button.
1 comentario
Walter Roberson
el 1 de Nov. de 2011
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
Respuesta aceptada
Micah
el 1 de Nov. de 2011
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).
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!