extracting variable from callback into base workspace

조회 수: 7 (최근 30일)
Abel Tan
Abel Tan 2018년 12월 27일
댓글: Abel Tan 2018년 12월 27일
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!

채택된 답변

Stephen23
Stephen23 2018년 12월 27일
편집: Stephen23 2018년 12월 27일
"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
Stephen23
Stephen23 2018년 12월 27일
편집: Stephen23 2018년 12월 27일
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 2018년 12월 27일
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!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by