Passing base workspace variables to callback functions
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (2)
Kevin Holst
el 9 de Mayo de 2012
Have you tried using the evalin function, maybe something like:
x = evalin('base','var');
Walter Roberson
el 9 de Mayo de 2012
1 voto
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.
Categorías
Más información sobre Whos en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!