Multiple variable generated by one GUI element
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
CHAZAUD Mathilde
el 15 de Mayo de 2019
Respondida: CHAZAUD Mathilde
el 20 de Mayo de 2019
Hi everyone !
I'm running a programm to solve fluid mechanics equations with a GUI. I used the 'guide' tool to create it. Here is my problem:
I have several GUI windows to enter different types of parameters in order to solve my equations. I use the same GUI windows at different time in my program, for example with edit text boxes but I want to save each input in a different variable.
For example:
the first time I open my GUI figure, I enter 10 in the edit text box, it's saved as the 'x' variable.
Later in the programm I open the same GUI figure, but this time i want to save my input (20 for example) in a new variable 'x2'.
I hope I made my issue understanble !
Thanks for your help !
2 comentarios
Geoff Hayes
el 15 de Mayo de 2019
Chazaud - you may need to post some of your code. When you open the GUI a third time, will it overwrite one of the previous values or be a new value? If the latter, then I would just create an array in your handles structure from the parent GUI (?) that will store these different values.
Respuesta aceptada
Dennis
el 16 de Mayo de 2019
Most likely you do not want to store each value in a different variable. Please check Stephen's tutorial to learn more about this.
You can use 1 variable and indices to store all your values, this way it is not only easier to store them, but also to use them or pass them to other functions:
%create two UI elements:
handles.uihandles(1)=uicontrol('Style','edit','String','Para 1','Position',[20 200 100 40]);
handles.uihandles(2)=uicontrol('Style','pushbutton','String', 'Store Value','Callback',{@simpleUI_callback,handles},'Position',[20 300 100 40]);
function simpleUI_callback(hObj,~,handles)
nx=getappdata(hObj,'nx'); %retrieve nx
nx(end+1)=str2double(handles.uihandles(1).String); %append new value to nx
handles.uihandles(1).String=sprintf('Para %d',numel(nx)+1); %change String of edit field - not needed, cosmetic
disp(nx)
setappdata(hObj,'nx',nx) %store nx
end
2 comentarios
Dennis
el 16 de Mayo de 2019
Editada: Dennis
el 16 de Mayo de 2019
Where did you place N=size(nx)?
My best guess is that you tried something like
nx=getappdata(hObj,'nx');
N=size(nx)
in another function/callback. In that case it should return 0 0.
In my example i stored nx in the pushbutton. If you want to access nx from another function or callback you need to pass the correct handle. In my case that would be handles.uihandles(2).
nx=getappdata(handles.uihandles(2)),'nx'); %probably handles.pushbutton1 in your case
Building on my previous example:
handles.uihandles(1)=uicontrol('Style','edit','String','Para 1','Position',[20 200 100 40]);
handles.uihandles(2)=uicontrol('Style','pushbutton','String', 'Store Value','Callback',{@simpleUI_callback,handles},'Position',[20 300 100 40]);
handles.uihandles(3)=uicontrol('Style','pushbutton','String','Calculate!','Position',[20 100 100 40]);
handles.uihandles(3).Callback={@simpleUI_calculate_callback,handles};
function simpleUI_callback(hObj,~,handles)
nx=getappdata(hObj,'nx'); %hObj in this case is handles.uihandles(2), where nx is stored
nx(end+1)=str2double(handles.uihandles(1).String);
handles.uihandles(1).String=sprintf('Para %d',numel(nx)+1);
%disp(nx)
setappdata(hObj,'nx',nx) %the hObj in this case is handles.uihandles(2), nx is stored in this object
end
function simpleUI_calculate_callback(~,~,handles)
nx=getappdata(handles.uihandles(2),'nx'); %we need to retrieve nx from handles.uihandles(2), because the hObj in this callback is handles.uihandles(3)
fprintf('nx has a size of %d %d\n',size(nx))
disp(nx)
end
Más respuestas (1)
Ver también
Categorías
Más información sobre Migrate GUIDE Apps 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!