Passing base workspace variables to callback functions
Show older comments
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.
Accepted Answer
More Answers (2)
Kevin Holst
on 9 May 2012
Have you tried using the evalin function, maybe something like:
x = evalin('base','var');
Walter Roberson
on 9 May 2012
1 vote
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.
Categories
Find more on Whos 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!