Multiple variable generated by one GUI element

3 ビュー (過去 30 日間)
CHAZAUD Mathilde
CHAZAUD Mathilde 2019 年 5 月 15 日
回答済み: CHAZAUD Mathilde 2019 年 5 月 20 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 5 月 15 日
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 2019 年 5 月 16 日
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

サインインしてコメントする。

採用された回答

Dennis
Dennis 2019 年 5 月 16 日
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 件のコメント
CHAZAUD Mathilde
CHAZAUD Mathilde 2019 年 5 月 16 日
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 2019 年 5 月 16 日
編集済み: Dennis 2019 年 5 月 16 日
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

サインインしてコメントする。

その他の回答 (1 件)

CHAZAUD Mathilde
CHAZAUD Mathilde 2019 年 5 月 20 日
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 !

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by