Input and Output to and from workspace from running gui

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

Maximilian - are these datasets files? How are they used outside of the GUI?
They are global structs containing cell arrays, cells, strings and numeric data.
To be clear - the user can change the datasets in the workspace (presumably from the command line) or through the GUI, and that you wish that the change be made in either direction as soon as possible? Is that the case? Why do you allow the user to update the datasets in both places - can't you just use the GUI to make all of the updates? You may need to provide some context so as to better illustrate your use case.
Thanks for the detailed question, I will try to illustrate it better.
The large structs contain information as well a data from eletrophysiological experiments, only one struct exists at a time in the workspace and is made a global variable. Functions to analyse these data are getting and setting values and data to the struct. Usually I declare this varible global in all functions that directly need to access it. It might not be the best style of all time, but it is the only global variable that I use and it works just fine.
I have now introduced a couple of values that describe parts of the data, for example saying "low SNR". The input is done by the user (mostly me) and would require tedious typing and writing the values to their fields by hand from the command window.
I thought that a GUI would be a good way to have some checkboxes and simple numeric inputs to both SHOW the existing values and SET or CHANGE them.
So the GUI must do the following: 1) Retrieve the values from the global struct and present them in the GUI (this is not a problem an I know how to do it) 2) Write the data to the global struct WITHOUT closing the GUI. (This part I need help with) The idea is that the GUI is always open, even if the datasets are changed.
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:
@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.

Categories

Asked:

on 9 Feb 2017

Edited:

on 16 Feb 2017

Community Treasure Hunt

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

Start Hunting!