GUI question: a button once clicked allows the user to choose a variable from the base workspace to load.
7 次查看(过去 30 天)
显示 更早的评论
Hi!
I have been trying to make a button that lets the user select a variable to be used in the GUI. From reading online, I only find the ''evalin'' function to be a variable importer. Thing is, it requires the variable name to be known. Different users might have different names for the variable that they want to import.
What I would like is similar to the Deep Learning Toolbox option where it shows you variable names from the base workspace, and you can select one.
I have been trying using ''whos'' to find the variables names in the base workspace, but it will show the GUI workspace variables instead of the base workspace variables.
Any help would be appreciated!
0 个评论
采纳的回答
Chandler Hall
2022-11-14
编辑:Chandler Hall
2022-11-14
You can obtain a cell array of the names of variables in the base workspace with the command evalin('base', 'who'). You can then use those strings in evalin('base', str). For example:
% Add some variables to the base workspace yourself
function access_base_vars
f = figure('defaultuicontrolunits', 'normalized');
base_vars = evalin('base', 'who');
gui.menu = uicontrol('style', 'popupmenu', 'string', base_vars, 'callback', @update, 'position', [0.05 0.5 0.2 0.1]);
gui.var_class = uicontrol('style', 'text', 'string', 'Variable Class: ', 'position', [0.3 0.5 0.5 0.1]);
guidata(f, gui);
function update(obj, ~)
str = obj.String{obj.Value};
gui.var_class.String = 'Variable Class: ' + string(class(evalin('base', str)));
end
end
In an actual scenario, you will want to ensure you recalculate evalin('base', 'who') every time before using the strings as the user could have altered the workspace.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!