Return values from uifigure by pressing OK button

조회 수: 40 (최근 30일)
Julia Fischer
Julia Fischer 2022년 7월 14일
편집: Jonas 2022년 7월 14일
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;
...

채택된 답변

Jonas
Jonas 2022년 7월 14일
편집: Jonas 2022년 7월 14일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by