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!

采纳的回答

Chandler Hall
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 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by