필터 지우기
필터 지우기

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

조회 수: 3 (최근 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