Input and Output to and from workspace from running gui

조회 수: 8 (최근 30일)
Maximilian Rüppell
Maximilian Rüppell 2017년 2월 9일
편집: per isakson 2017년 2월 16일
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
Stephen23
Stephen23 2017년 2월 15일
편집: Stephen23 2017년 2월 15일
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 2017년 2월 16일
@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.

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

답변 (1개)

Saurabh Gupta
Saurabh Gupta 2017년 2월 15일
편집: per isakson 2017년 2월 16일

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by