I wrote a simple function which incorporates creation of a figure and pusbutton.
I expected that after pressing the button, the function will provide the answer based on what callcack function of pushbutton is performing:
function [B,success] = TGA_MAIN(X,Y,L,W,Xb,Yb,Lb,Wb, STRING)
global B
B=0,
success=0
Xa=X;
Ya=Y;
La=L;
Wa=W;
p=Xb;
r=Xb;
s=Lb;
t=Wb;
STR=STRING;
handles.load_fig=figure('MenuBar','none','Position',[Xa,Ya,La,Wa])
handles.load_button=uicontrol(handles.load_fig,'style','pushbutton','string',STR,'position',[p,r,s,t])
set(handles.load_button,'callback',{@load_callback,handles})
function load_callback(gcf,event_data,handles)
global B
x=5;
B=x;
success=1;
My expectation was that after I press button, the answer must be q=5,s=1.
However, I see q=0,s=0, which I defined in very beginning of soft.
The pressing of the button changes nothing...
Example of call:
[q,s]=TGA_MAIN(500,400,400,300,120,120,120,50, 'STRING')
result is:
q =
0
s =
0

댓글 수: 1

Rik
Rik 2021년 9월 26일
You shouldn't use globals to share data to a callback. Use the guidata struct instead. For general advice and examples for how to create a GUI, have look at this thread.

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

 채택된 답변

Sergey Lopatnikov
Sergey Lopatnikov 2021년 9월 26일

0 개 추천

Thank you. It works

추가 답변 (4개)

Image Analyst
Image Analyst 2021년 9월 26일

0 개 추천

Set a breakpoint in the callback function. Does it stop there? What happens if you step through it line by line?

댓글 수: 4

Sergey Lopatnikov
Sergey Lopatnikov 2021년 9월 26일
It seems that by some reason callback function does not see the press botton action.
I slightly modified program - put end-s at the end callbacj funtion and main function like this:
function [B,success] = TGA_MAIN(X,Y,L,W,Xb,Yb,Lb,Wb, STRING)
global B
global success
B=0,
success=0
Xa=X;
Ya=Y;
La=L;
Wa=W;
p=Xb;
r=Xb;
s=Lb;
t=Wb;
STR=STRING;
handles.load_fig=figure('MenuBar','none','Position',[Xa,Ya,La,Wa])
handles.load_button=uicontrol(handles.load_fig,'style','pushbutton','string',STR,'position',[p,r,s,t])
set(handles.load_button,'callback',{@load_callback,handles})
x=0;
function load_callback(gcf,event_data,handles)
x=5
B=x
success=1
end
end
However if I call:
[q,s] = TGA_MAIN(500,400,400,300,120,120,120,50, 'LOAD')
I not see q and s. Only x,B and "success" "inside the callback" , but not as the main function exit.
And is I use x=5; B=x; success=1; - I see nothing inspite I defines B and success as global
Image Analyst
Image Analyst 2021년 9월 26일
편집: Image Analyst 2021년 9월 26일
If you right click on the push button in GUIDE and select Callback->Ciew Callback, it should bring you to the callback function that actually gets called. Apparently it's a different one that you think. It should be called something like
function btnAnalyze_Callback(hObject, eventdata, handles)
where btnAnalyze is replaced by whatever your actual "Tag" property for that button is. Do that and step through. Do you ever get to the code you think you should?
And also put a breakpoint in TGA_MAIN and btnAnalyze_Callback.
So, when you click on the pushbutton, do you stop in btnAnalyze_Callback or TGA_MAIN?
Sergey Lopatnikov
Sergey Lopatnikov 2021년 9월 26일
I do not use GUIDE at all. I write from scratch using handles,
handles.load_fig=figure('MenuBar','none','Position',[Xa,Ya,La,Wa])
handles.load_button=uicontrol(handles.load_fig,'style','pushbutton','string',STR,'position',[p,r,s,t])
set(handles.load_button,'callback',{@load_callback,handles})
function load_callback(gcf,event_data,handles)
x=5;
B=x;
success=1;
end
Image Analyst
Image Analyst 2021년 9월 26일
OK you're doing it the harder way. But the question is the same. Set a breakpoint there and see if you step into that function. Do you?

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

Sergey Lopatnikov
Sergey Lopatnikov 2021년 9월 26일

0 개 추천

:). To my view it is way easier and more flexible way.
Yes, it nornaly stops at preack poin.
The proble is how to transfer data from inside of callback function to main function?
I thought that B and "success" wich I defines ad global must be visible in main function. Howvever by some reason they are not.

댓글 수: 1

Global does not automatically make a variable global everywhere. Any place that you want a globa variable to be seen, you must have
global B
global success
otherwise they'll be local variables, not the global variables you expected.

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

Sergey Lopatnikov
Sergey Lopatnikov 2021년 9월 26일

0 개 추천

It does not work like this.
If I try to define global in main AND within call back, it gives error:
[q,s] = TGA_MAIN(500,400,400,300,120,120,120,50, 'LOAD')
Error: File: TGA_MAIN.m Line: 28 Column: 12
Global or persistent variables must be declared in the same scope where they are first used. Move the
declaration for variable 'B' to the outermost function before its first use.
Again: I see corect data within callback, however, I do not know how make those data available outside of call back for main?

댓글 수: 1

Image Analyst
Image Analyst 2021년 9월 26일
You posted this as an "Answer", (rather than as a comment to my Answer), so have you answered it?
Is the code in your original post your entire program?

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

Steven Lord
Steven Lord 2021년 9월 26일

0 개 추천

MATLAB is behaving correctly. You create the pushbutton and give it a callback and then immediately exit from the function in which it was created, returning the values of the outputs that they have at that time. By default MATLAB will not wait for you to push the button (and so update the global variable) in the workspace of that function. You could try to make MATLAB wait using something like uiwait so the TGA_MAIN function doesn't return until the figure with that button gets closed or some property updated.

카테고리

도움말 센터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!

Translated by