Input and Output to and from workspace from running gui

8 views (last 30 days)
Hi everyone, I have been searching for hours, maybe days to solve a problem and I hope for your help!
I have many datasets, each one containing of a large struct with lots of information and data in it. I am about to write a GUI that accesses parts of the information in the struct and shows them to the user, who can change these data. Afterwards they whould be save to the struct. I would like the GUI to stay open/running and update constantly in both directions. And I don't not how to pass varibles/values to and from a GUI while it is running. What could I do? Any help is appreciated!
Some more detailed description:
Dataset1 with fields
data.text.a='foo'
data.text.b='bar'
The GUI should present these data to the user. If the user changes something ('foo' to 'fire'), it should be written to the Dataset1:
data.text.a='fire'
whithout closing the GUI. If the Dataset is changed to Dataset2, these Data should be displayed in the GUI. I know that I could to the constant updating with a timer object. But as I said: I don't know how to pass variables to or from running GUI.
I have tried global variables and interestingly this does work, if you set a breakpoint in the callback function of respective uicontrol. I don't know why though... Ah, and I have started building the GUI without GUIDE but I don't have any preferences there.
  6 Comments
Stephen23
Stephen23 on 15 Feb 2017
Edited: Stephen23 on 15 Feb 2017
Note that trying to operate on data in the workspace like that is always going to be inefficient. A much faster and more robust solution would be to pass those variables as arguments to a function, and let the GUI access them from the workspace of that function (e.g. using nested functions or guidata). In that way slow and buggy tools like evalin and assignin can easily be avoided. Note that the documentation lists passing arguments as the "BestPractice" for passing data between workspaces:
In comparison using assignin makes variables magically "poof" into existence in some workspace, which risks overwriting existing data without warning, and also is slow because JIT does cannot optimize these operations:
Using global variables suffers from a very high risk of uncontrolled data changes which are almost impossible to debug. Expert programmers avoid using globals in their own code, and recommend that beginners learn to avoid global too:
Also note that MATLAB does not automatically copy the data when it is passed as an argument to a function so passing large arrays is not a problem:
You will see that global's and using eval (includes its siblings evalin and assignin) come at the top two places on this list of bad code practices:
Maximilian Rüppell
Maximilian Rüppell on 16 Feb 2017
@Stephen: Thanks for the nice collection, but this is close the usual:
"I have a question concerning windows" - "Use linux instead!"
I know that globals can be dangerous, but since I am only changing small parts of a very large struct most of the time, it might be better than passing the whole struct. Anyway: Changing this is not an option at the moment, but I hope to get some more ideas for my initial question.

Sign in to comment.

Answers (1)

Saurabh Gupta
Saurabh Gupta on 15 Feb 2017
Edited: per isakson on 16 Feb 2017

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!