Problem of using "assignin" during figure callbacks

14 次查看(过去 30 天)
Hi every member,
Overall, I want to get a output from a figure callback after the figure closed. I am doing it by using "assignin". But during I used it, I found a curious problem. Even by assignin('caller'), which is suppose to be a function workspase, the argument always be sent to the base workspace. I have no idea why it happens in this way. Following is my code to illustrate my problem in details. The variable "rx" and "imgx" is a contrast to show my problem.
>> make_img % this is the input command in matlab command line
% this function is to make a "img" variable and calling "test" function
function make_img
radius=100;
c0=[radius+1,radius+1];
[X,Y] = ndgrid(1:2*radius+1) ;
img = zeros(2*radius+1);
img(sqrt((X-c0(1)).^2 + (Y-c0(2)).^2) <= radius) = 1;
test(img) % call "test"
end
% this is the called function
% it suppose to give the "caller" two variable, rx and imgx, not to the base
function test(img)
r=20;
assignin('caller','rx',r)
h=imshow(img);
fig = gcf;
fig.UserData.img=img;
fig.WindowKeyPressFcn=@key;
function key(fig,~)
img=fig.UserData.img;
a=fig.CurrentCharacter;
if isequal(a,'q')
assignin('caller','imgx',img);
delete(fig)
return
end
end
end
The output in base workspace is suppose to be none. But the "imgx" exist and "rx" not. It seems the caller of the callback is always the base workspace? I am confused about it. If it is so, how could I get a output after the figure deleted in the subfunction workspace?
Thank you.
  7 个评论
wei zhang
wei zhang 2019-6-5
Thank you for your greart advise! This is exactly what I need, the "waitfor" function. I have never used it before. A really neat way instead of "assignin". It is a pity could not accept your comment as an answer. : )
Stephen23
Stephen23 2019-6-5
"It is a pity could not accept your comment as an answer"
You can now :)
I am glad that it helped!

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2019-6-5
You just need to add waitfor to your code:
function out = test(...)
out = [];
fgh = figure(...);
...
function mycallback(...)
out = 'hello world';
end
...
waitfor(fgh) % return only when figure is closed.
end

更多回答(1 个)

Jan
Jan 2019-6-4
Exactly: Callbacks are called from the base workspace.
"how could I get a output after the figure deleted in the subfunction workspace?"
It depends on where you want which output. You can simply write in a message in the command window from inside the key() function. But if you want to create a variable in the base workspace, the program design is inefficient. Using the base workspace as a collector of variables is not smart. Better create a GUI and keep it open as long as the processing is going on. Then store the variables inside the GUI, e.g. using guidata. This allows you e.g. to run multiple instances of the code without collisions by a polluted base workspace.
  1 个评论
wei zhang
wei zhang 2019-6-5
At first, thank you for your reply. You seems to clarify the impossibility of storing a variable as a output. However, I could not always keep the gui opening. I need to save some data after I close it. If I close the gui, the "guidata" disappear, right? Do you mean there is no way to output the data from gui?
"You can simply write in a message in the command window from inside the key() function." Does it mean I can use "disp" or other display function to give a note?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by