extracting variable from callback into base workspace

6 views (last 30 days)
Hello,
I have a script that creates a dialog box, with all UIControl element declaration in a function - editdialogbox.
All callbacks are nested inside editdialogbox, so I have no problem passing/changing variables among callbacks.
In my dialog box, I have a pushbutton with a callback that:
  1. computes a variable laserSpot using data from edit boxes in the dialog box
  2. and then closes the dialog box.
In my main script, I have the code: waitfor(editdialogbox), so that the script only continues to run after the dialog box is closed. The problem is, the variable laserSpot is required in the later part of the code. How can I pass the variable into the base workspace?
I have read documentation on how to share data between workspaces, share data between callbacks, and it seems assignin() is probably the option to go. However, I have seen experts saying assignin() is not a good suggestion. What else can I do? and when is it actually alright to use assignin()?
Hope my problem is clear!
Thanks!

Accepted Answer

Stephen23
Stephen23 on 27 Dec 2018
Edited: Stephen23 on 27 Dec 2018
"However, I have seen experts saying assignin() is not a good suggestion."
assignin should definitely be avoided, as should any other function that makes data magically appear or magically disappear from any workspace. Read this to know why:
"What else can I do?"
I find the easiest way to return values from a GUI is to use nested functions for the callbacks and then simply return any required variables as output arguments of the main function:
function [X,Y] = mainfun(...)
X = [];
Y = [];
... nested callback functions set the values of X and Y
waitfor(figure_handle)
end
And then you can simply call that function wherever you need to:
[Xout,Yout] = mainfun(...)
For an example of this see my FEX submission iregexp (you will need to uncomment the waitfor line indicated inside the file):
"and when is it actually alright to use assignin()?"
Rarely. The trivial case of passing data from one workspace to another is easily done using more reliable methods: passing data as output arguments is simple, neat, and efficient.
  3 Comments
Stephen23
Stephen23 on 27 Dec 2018
Edited: Stephen23 on 27 Dec 2018
You will need to have a main function (which may be your existing function or a small wrapper function), within which you define the required variable/s, call waitfor, and also return those variables as ouptut arguments. To make this work in your case you will need to either include waitfor inside of editdialogbox or put it all inside a simple main function (which sets up the GUI, variables and then waitfor). They both amount to much the same thing.
Do not call that main function multiple times, only once (or whenever you need that GUI): the waitfor (inside the main function) will make everything wait until that dialog box is closed, and then will return those output arguments (which you defined using nested callback functions) from the main function.
Take a look at my iregexp code, which returns values when the GUI is closed.
Abel Tan
Abel Tan on 27 Dec 2018
ahhh..
ok the waitfor is inside the function. I missed that part in your answer previously. Ok that explains a lot.
I'm sorry I didn't take time to look at your code previosuly because this wasn't the only task on hand and it was a lengthy code as well. I've just went to take a look and changed my code to how yours was written.
Thanks so much!

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!