Return values from uifigure by pressing OK button

30 vues (au cours des 30 derniers jours)
Julia Fischer
Julia Fischer le 14 Juil 2022
Modifié(e) : Jonas le 14 Juil 2022
Hello
I have a simple form composed of 2 uitextarea and an OK button
How can I tell the form to return the values of the text areas (whatever is inside ) to the workspae or a client function and closing the form after that , more or less like an inputdialog. I am not using the input dialog because I do have two buttons that fill the text areas. A short version follows:
Thanks!!
Julia
function myOutput=test()
mainFig=uifigure;
mainFig.Visible='off';
txt_pos=[30 210 100 40];
txtFile=uitextarea(mainFig,'Position',txt_pos);
txtFile.Editable='on';
txt_pos=[30 150 100 40];
txtCnf=uitextarea(mainFig,'Position',txt_pos);
txtCnf.Editable='on';
btn_pos=[300 50 100 40];
btnOk=uibutton(mainFig,...
'push',...
'Position',btn_pos,...
'ButtonPushedFcn',@(btnOk, event) pushBtOk(btnOk,mainFig,txtCnf,txtFile));
mainFig.Visible='on';
function pushBtOk(btn,mainfig,txtCnf,txtFile)
disp( txtCnf.Value);
disp( txtFile.Value);
%%put Values into myOutput
%%close(uifigure)
end
end
the client code
...
myOutput=test;
...

Réponse acceptée

Jonas
Jonas le 14 Juil 2022
Modifié(e) : Jonas le 14 Juil 2022
if you nest functions in functions, you can directly use the defined variables. note also the uiwait to wait for the functio nreturn until the window was closed
function myOutput=test()
mainFig=uifigure;
mainFig.Visible='off';
txt_pos=[30 210 100 40];
txtFile=uitextarea(mainFig,'Position',txt_pos);
txtFile.Editable='on';
txt_pos=[30 150 100 40];
txtCnf=uitextarea(mainFig,'Position',txt_pos);
txtCnf.Editable='on';
btn_pos=[300 50 100 40];
btnOk=uibutton(mainFig,...
'push',...
'Position',btn_pos,...
'ButtonPushedFcn',@(~, ~) pushBtOk(txtCnf,txtFile));
mainFig.Visible='on';
uiwait(mainFig);
function pushBtOk(txtCnf,txtFile)
disp( txtCnf.Value);
disp( txtFile.Value);
myOutput(1)=txtCnf.Value;
myOutput(2)=txtFile.Value;
%%put Values into myOutput
close(mainFig)
end
end
try it using
twoFields=test();
you could even call the function without further arguments
'ButtonPushedFcn',@(~, ~) pushBtOk());
and
function pushBtOk()
%....
end

Plus de réponses (0)

Catégories

En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by