Passing base workspace variables to callback functions

14 ビュー (過去 30 日間)
Michael Joslin
Michael Joslin 2012 年 5 月 9 日
I have created a GUI using the UI commands and I want to pass variables from the base workspace into the callback function for one of the buttons to use in calculations when I click it.
uimenu(menu_options_h,'Label', 'Save Calibrations','Callback', {@cal_save});
function load_cals_callback(source, eventdata) %#ok<INUSD>
_Some Code_
end
I have tried {@cal_save(x)}, {@(x) cal_save,x} and some others. One solution I can think of is to make all of my base workspace variables global but that would be bad programming practice.

採用された回答

Daniel Shub
Daniel Shub 2012 年 5 月 9 日
What you are trying to do is difficult because it is bad programming practice with or without using globals. Basically you are trying to poof variables from your base workspace into you callback workspace.
A better approach would be to load the variables in a controlled manner into the GUI workspace and then pass them into the callback workspace. The loading into the GUI could be through text boxes, a load file box, or even a load data box (where evalin might be both useful and acceptable). Once loaded into the GUI you can share them with the callback like any other data.
  1 件のコメント
Michael Joslin
Michael Joslin 2012 年 5 月 9 日
I never thought of this, alright I'll give it a shot!

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

その他の回答 (2 件)

Kevin Holst
Kevin Holst 2012 年 5 月 9 日
Have you tried using the evalin function, maybe something like:
x = evalin('base','var');

Walter Roberson
Walter Roberson 2012 年 5 月 9 日
The only way for an anonymous function to "capture" a reference to the base workspace is for the anonymous function to be created inside a script that is executed before any function is created. You would need a "get" function and a "set" function if you wanted to be able to read and write the variable.
Is there a reason that it is necessary that the base workspace be the one? It would make more sense (I think) to use nested routines and shared variables.

カテゴリ

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