MATLAB Answers

Multiple variable generated by one GUI element

9 views (last 30 days)
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 Comments

Geoff Hayes
Geoff Hayes on 15 May 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.
CHAZAUD Mathilde
CHAZAUD Mathilde on 16 May 2019
Thank you for your answer,
here is for example the code of my edit text box called 'nx'
function nx_Callback(hObject, eventdata, handles)
% hObject handle to ny (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ny as text
% str2double(get(hObject,'String')) returns contents of ny as a double
nx=str2double(get(handles.nx,'String'));
setappdata(0,'nx',nx);
assignin('base','nx',nx);
I would like that, if I enter a new value in this edit box, it creates a new variable 'nxi' to save the new data within erase the previous nx

Sign in to comment.

Accepted Answer

Dennis
Dennis on 16 May 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 Comments

CHAZAUD Mathilde
CHAZAUD Mathilde on 16 May 2019
Thank you very much for your help Dennis !
just one more question: I don't really understand how it 'works'.
I used the code you wrote, i entered 3 times my nx parameters. so I have nx=[0 1 2], but if I write N=size(nx), it says N=0.... I should have N=3, shouldn't I ?
why ?
Dennis
Dennis on 16 May 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

Sign in to comment.

More Answers (1)

CHAZAUD Mathilde
CHAZAUD Mathilde on 20 May 2019
I managed to do what I wanted by using
nx=getappdata(handles.nx(1),'nx');
I needed that to do a loop like ' for i = 1:size(nx)'
Thank you for your help !

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by