guidata() not working as expected
이전 댓글 표시
The below code is a basic example of my question,. This code generates a simple pushbutton and a display box to display the Value of the pushbutton. This is expected to read "0" when the code is run, as the pushbutton defaults to a value of 0 upon running. When the button is pressed (button is labeled 'Flight'), the callback function is called, where the pushbutton's value is updated to be 1 manually, and the display string is updated to be this new pushbutton value. I have commands to print to the command window the values of both of these variables before the button is pressed, as well as after.
My question is: when the button is pressed, the display string is updated to be "1" but the final pushbutton value is still 0. Why is this? Why is it not "1" as it's been assigned? Thanks
close all
% create figure
f = figure;
% create a pushbutton
poo.Button = uicontrol('Style','pushbutton', ...
'Parent',f,...
'Position',[20,20,40,40],...
'String', 'Flight',...
'HorizontalAlignment', 'center',...
'Callback',{@continueFcn});
% create a display box for the value of the pushbutton
poo.disp = uicontrol('Style','text',...
'Parent',f, ...
'Position',[80,80,20,20],...
'String', num2str(poo.Button.Value),...
'HorizontalAlignment', 'center');
% store the poo struct as the handle of the figure, f
guidata(gcf,poo);
% print the inital values of the display string and button value
fprintf(poo.disp.String)
display(poo.Button.Value)
% figure waits
uiwait(gcf)
% retrieve the new data from the figure
poo2 = guidata(gcf);
% print the updated values of the display string and button value
fprintf(poo2.disp.String)
display(poo2.Button.Value)
% callback for the pushbutton
function continueFcn(~,~)
poo2 = guidata(gcf);
% set the button value to be 1
set(poo2.Button,'Value',1)
% set the display string to be value of the pressed button -- 1
set(poo2.disp,'String',num2str(poo2.Button.Value))
% store the new poo2 struct as the gcf's data
guidata(gcf,poo2)
% resume
uiresume(gcf)
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!