フィルターのクリア

Passing variables in GUI vs. assignin then evalin

6 ビュー (過去 30 日間)
Dc215905
Dc215905 2020 年 3 月 11 日
コメント済み: Dc215905 2020 年 3 月 13 日
I've looked through multple ways to do this on the forum but I still can't seem to figure it out.
I'm loading a bunch of variables into my GUI that I want to be able to pass to different functions/callbacks (they are the same, right?).
In the past I have done this by:
[fname, path]=uigetfile('*.mat');
File = fullfile(path, fname);
load(File)
assignin('base','com',com);
assignin(many more)
and then in another callback:
com = evalin('base','com')
manymore = evalin('base','manymore')
How do I pass these individual variables between different callbacks so I don't have to always pass it throught the workspace and bring int back (also, what's wrong with doing it this way?)?
I know guidedata(hObject, handles) exist, but I can't for the life of me make it work.
Thanks
  2 件のコメント
Stephen23
Stephen23 2020 年 3 月 11 日
編集済み: Stephen23 2020 年 3 月 11 日
"How do I pass these individual variables between different callbacks so I don't have to always pass it throught the workspace and bring int back"
Use nested functions (if you are sensibly writing your own code) or guidata (if unfortunately using GUIDE):
This forum also has plenty of working examples. This is a good tutorial to start with:
"...also, what's wrong with doing it this way?"
Your current approach is slow, inefficient, obfuscates the code intent, is liable to bugs, and makes debugging harder. Really there is not much to recommend it, but it does seem to be popular with beginners who like everything to magically appear in the base workspace. Your approach will make writing generalizable, efficient, testable code more difficult.
Dc215905
Dc215905 2020 年 3 月 13 日
Thank you! There's a lot of valuable info in this post.

サインインしてコメントする。

採用された回答

Rik
Rik 2020 年 3 月 11 日
編集済み: Rik 2020 年 3 月 11 日
To answer the question of how to do it with guidata:
%this stores data to your figure:
guidata(hObject,handles)
%this retrieves that data:
handles=guidata(hObject)
In your function you can modify or create fields of the struct. If you modify data, don't forget to use the first to update the data stored with the figure.
You can treat the first line as a set() and the second line as a get(). If you have a GUI created with GUIDE the second line is implicitly executed every callback, so you'll only need to use the first line.
  1 件のコメント
Dc215905
Dc215905 2020 年 3 月 13 日
Thanks for the simple explanation. Your set() and get() comment made perfect sense.

サインインしてコメントする。

その他の回答 (1 件)

Peter O
Peter O 2020 年 3 月 11 日
Nested Functions are your favorite friend when working with GUIs. Place the callbacks within the main UI, and they'll be able to access the scope of the parent function.
And:
Whenever possible avoid using evalin and evalc. They permit execution of arbitrary code (including shell commands), so they can be a security risk.
For scoping reasons, it's "dangerous" to place variables into global namespace because they can be accessed and modified by other functions that you might hypothetically have running, and it creates a lot of memory access overhead. And they might overwrite your poor user's code if names overlap. For instance, if you have the variable in the UI called "data," there's a high probability it might overwrite the information the user just loaded in the workspace from data.mat :)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by