Return values from uifigure by pressing OK button

27 Ansichten (letzte 30 Tage)
Julia Fischer
Julia Fischer am 14 Jul. 2022
Bearbeitet: Jonas am 14 Jul. 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;
...

Akzeptierte Antwort

Jonas
Jonas am 14 Jul. 2022
Bearbeitet: Jonas am 14 Jul. 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

Weitere Antworten (0)

Kategorien

Mehr zu Develop uifigure-Based Apps finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by