open a variable in a function

Hello,
I have a dialog box created within a function. Within the dialog box is a pushbutton with a callback that is planned to create a variable and then opens it in the variable editor, all this while the dialog box is still active. If i close the variable editor and press the same button again, it should open the variable in the variable editor again.
I have tried using openvar() but openvar opens variables from the workspace in scope, which is the 'base' workspace during a callback. The variable is created in the callback and thus openvar() does not work for me.
how can I open the variable created in the callback in the variable editor without the use of evalin or assignin?
Thanks!

2 comentarios

Walter Roberson
Walter Roberson el 31 de En. de 2019
Editada: Walter Roberson el 31 de En. de 2019
The scope for callbacks is the base workspace only for callbacks specified as character vector or a cell array in which the first element is a character vector .otherwise the scope would be the function that is the callback .
Abel Tan
Abel Tan el 31 de En. de 2019
thanks for correcting me on that!

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 31 de En. de 2019
Editada: Jan el 31 de En. de 2019
I cannot try it currently. Although the callback is called from the base workspace, the current workspace inside the callback is the callback's one. So you can create the variable there:
function YourCallback(hObject, EventData, handles)
X = rand(2, 2);
openvar('X');
pause(20); % For this demonstration only!!!
end
Does this work? Then the assumption, that the Base workspace is used, is not correct. Is your problem solved then?

7 comentarios

Guillaume
Guillaume el 31 de En. de 2019
You're correct, openvar only uses the current workspace (the callback).
With your example, after 20 seconds, the variable will disappear from the editor (if pause is on, if not, it disappears straight away). I'm not sure it's a good idea to prevent a callback from completing until a user interaction is finished. It may freeze the rest of the UI/matlab.
Abel Tan
Abel Tan el 31 de En. de 2019
Hi Jan,
thanks for the reply.
you code is similar to mine. the variable that I am trying to open is created in the callback function.
i even added codes above and below to make sure it was running, like this:
variable = ..%some arithmetic
...
exist('variable','var')
openvar('variable')
exist('variable','var')
...
both exist shows 1 in the command window
but still, when the variable editor open, it shows the variable does not exist
Guillaume
Guillaume el 31 de En. de 2019
when the variable editor open, it shows the variable does not exist
The variable only exists for the duration of the callback. If you put a breakpoint on the end statement of your callback, you'll see that when it's hit, the variable editor does show your variable. However, if you then let execution continue, the variable is destroyed together with the callback workspace as soon as that end statement is executed.
Jan
Jan el 31 de En. de 2019
So keep the workspace alive. Use uiwait to wait until the variable editor is closed.
I do not like GUIs, which block Matlab. Therefore openvar has a severe drawback and I'd prefer an uitable also.
Abel Tan
Abel Tan el 1 de Feb. de 2019
I've tried using uiwait, but when the variable editor opens, nothing is showing/loading. it is only after I've closed the ui that the somethings shows in the variable editor, and it is unfortunately "The variable does not exist".
I will take uitable into consideration. Thanks a lot for the help and advice! Really appreciate it!
Also, since there is no definite answer to this issue, is it alright if I leave it without accepting an answer?
Guillaume
Guillaume el 1 de Feb. de 2019
Possibly a drawnow before calling uiwait may display the variables. However, it's never a good idea to suspend a UI callback in order to allow more UI interaction so it may be that it doesn't work either. I think it's clear that the variable editor is not meant to be used as part of a GUI.
is it alright if I leave it without accepting an answer?
Maybe write your own answer summarising the discussion and accept it. Just so, the question has some sort of closure.
Jan
Jan el 1 de Feb. de 2019
"is it alright if I leave it without accepting an answer?"
Yes, this is okay. It is useful for the forum, if you write your own answer and accept this if you've found a working solution.

Iniciar sesión para comentar.

Guillaume
Guillaume el 31 de En. de 2019

0 votos

"the workspace in scope, which is the 'base' workspace during a callback"
Not at all. The workspace in scope is that of the callback, not the base workspace. However, that workspace is very short lived and is destroyed as soon as the callback completes. So any callback variable shown in the variable editor won't be displayed anymore when the callback returns. Instead you'll see The variable xxx does not exist.
I don't think there's a way to use the variable editor in conjunction with a GUI. If there is, it's probably going to involve some ugly hacks. Perhaps your best bet would be to use a uitable instead.

1 comentario

Abel Tan
Abel Tan el 31 de En. de 2019
Hi Guillaume,
thanks for the advice.
unfortunately, uitable doesn't fit too well with what I want to do.
I am able to do it using assignin to assign the variable to the base workspace and then using openvar. However, asssignin is kind of a hack which isn't sometimes too easy to debug and troubleshoot when codes get lengthy because it sort of 'spoofs a variable out of nowhere', and kind of shows that much thought hasn't been put into my code.
as such, I would like to avoid it if possible.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 31 de En. de 2019

Comentada:

Jan
el 1 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by