Problem of using "assignin" during figure callbacks

12 visualizaciones (últimos 30 días)
wei zhang
wei zhang el 4 de Jun. de 2019
Comentada: wei zhang el 5 de Jun. de 2019
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 comentarios
wei zhang
wei zhang el 5 de Jun. de 2019
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 el 5 de Jun. de 2019
"It is a pity could not accept your comment as an answer"
You can now :)
I am glad that it helped!

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 5 de Jun. de 2019
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

Más respuestas (1)

Jan
Jan el 4 de Jun. de 2019
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 comentario
wei zhang
wei zhang el 5 de Jun. de 2019
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by