Get values from callback function

조회 수: 2 (최근 30일)
Felix Albrecht
Felix Albrecht 2019년 4월 4일
댓글: Jan 2019년 4월 5일
Hi,
I want to create a GUI with two checkboxes, but I don't how to get the values of my checkboxes from my callback function. After clicking on a pushbutton, the values of the checkboxes cbx1 and cbx2 are saved as logial values in val1 and val2. I want my main function to return val1 and val2; is there a way how to do this? I'm a very beginner with GUIs, so there's probably a much more elegant solution.
This is how my window looks like:
2019-04-04 15_27_55-.png
% Main function
function [val1,val2] = checkbox
% Create window:
Pix_SS = get(0,'screensize');
height = 100;
width = 220;
center = [(Pix_SS(3)-width)/2 (Pix_SS(4)-height)/2];
fig = uifigure('Position',[center width height]);
% Create check boxes:
cbx1 = uicheckbox(fig,'Text','Define Window?','Position',[55 70 120 15]);
cbx2 = uicheckbox(fig,'Text','Select Peaks?','Position',[55 40 120 15]);
% Create Pushbutton
btn = uibutton(fig,'Text','OK','Position',[55 10 102 20],...
'ButtonPushedFcn', @(btn,event) ButtonPushed(btn,cbx1,cbx2,fig));
end
% Callback function
function ButtonPushed(cbx1,cbx2,fig)
val1 = cbx1.Value;
val2 = cbx2.Value;
close(fig);
end
  댓글 수: 2
Jan
Jan 2019년 4월 4일
Note: You cann the callback by
ButtonPushed(btn,cbx1,cbx2,fig)
but the function header is
ButtonPushed(cbx1,cbx2,fig) % 1st input missing
But the solution does not need the inputs of the callback.
Adam
Adam 2019년 4월 4일
You may also find
doc questdlg
or
doc uiconfirm
useful for simple UI-based things like this. I generally very quickly run into wanting to do something more than the very basic functionality they offer so write my own anyway, but they can handle all this for you for a simple 2-option question.
questdlg is the old version and uiconfirm uses the newer style uifigure, but since it is a standalone transient dialog either works fine here.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2019년 4월 4일
편집: Jan 2019년 4월 5일
Use uiwait and uiresume to wait for the pressing of the button:
...
% Create check boxes:
cbx1 = uicheckbox(fig,'Text','Define Window?','Position',[55 70 120 15]);
cbx2 = uicheckbox(fig,'Text','Select Peaks?','Position',[55 40 120 15]);
% Create Pushbutton
btn = uibutton(fig,'Text','OK','Position',[55 10 102 20],...
'ButtonPushedFcn', {@ButtonPushed, fig});
uiwait(fig);
if isgraphics(fig)
val1 = cbx1.Value;
val2 = cbx2.Value;
delete(fig); % Or: close()
else % Figure was deleted manually using the close icon:
val1 = NaN;
val2 = NaN;
end
end
function ButtonPushed(btn, EventData, fig)
uiresume(fig);
end
  댓글 수: 2
Felix Albrecht
Felix Albrecht 2019년 4월 5일
Thanks! uiwait and uiresume works perfect for me.
As a note: I still have to hand over my figure to the callback function. Otherwise it doesn't work the way I want.
function [val1,val2] = checkbox
% Create window:
Pix_SS = get(0,'screensize');
height = 100;
width = 220;
center = [(Pix_SS(3)-width)/2 (Pix_SS(4)-height)/2];
fig = uifigure('Position',[center width height]);
% Create check boxes:
cbx1 = uicheckbox(fig,'Text','Define Window?','Position',[55 70 120 15]);
cbx2 = uicheckbox(fig,'Text','Select Peaks?','Position',[55 40 120 15]);
% Create Pushbutton
btn = uibutton(fig,'Text','OK','Position',[55 10 102 20],...
'ButtonPushedFcn', @(btn,event) ButtonPushed(fig));
uiwait(fig);
if isgraphics(fig)
val1 = cbx1.Value;
val2 = cbx2.Value;
delete(fig); % Or: close()
else % Figure was deleted manually using the close icon:
val1 = NaN;
val2 = NaN;
end
end
function ButtonPushed(fig)
uiresume(fig);
end
Jan
Jan 2019년 4월 5일
You are right: I've fixed the code and included the fig handle in the callback.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by