Clear Filters
Clear Filters

GUI question: a button once clicked allows the user to choose a variable from the base workspace to load.

5 views (last 30 days)
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!

Accepted Answer

Chandler Hall
Chandler Hall on 14 Nov 2022
Edited: Chandler Hall on 14 Nov 2022
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.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!