Numeric Editfield in App Designer

18 views (last 30 days)
Mert Aygün
Mert Aygün on 8 Feb 2020
Hi folks,
How can I write a workspace variable to a numeric editfield? I am trying to use app designer as a GUI and once I run my simulink model, results are going to be in the workspace then I would like to write some of the outputs to the related numeric editfield after running my app. Any idea will be appriciated. Thanks in advance.
Adam Danz
Adam Danz on 8 Feb 2020
Edited: Adam Danz on 11 Feb 2020
"results are going to be in the workspace"
Which workspace? The base workspace?

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 8 Feb 2020
Edited: Adam Danz on 9 Feb 2020
To select variables from the base workspace into app designer you need to run the who or the whos function in the base workspace to list the available variables. However, since a callback function is likely evoking this, you'll need to wrap the who/s function in evalin() which is looked down upon for good reason, but I don't see an alternative for this solution (you could, of course, pass the variables directly to the GUI has Stephen commented).
This demo lists all of the variables available in the base workspace and produces a UITable showing the variable name, size, bytes, and class of each variable. The UITable has a checkbox column that allows you to select what variables you want to load into the app.
See the inline comments for detail.
Load some data in your base workspace
Executed from the command window.
% Load some data into the workspace so we have some variables to work
% with. These are a builtin and provided by Matlab.
load patients
Get information about base workspace variables
This will be executed from your App. Perhaps a pushbutton callback will evoke this.
% Before calling the whos function from evalin, we'll do a
% security check that the whos function is the one provided
% by Matlab. This reduces the risk of using evalin().
if isempty(regexp(which('whos'), 'built-in *.'))
error('The built-in whos() function is being overshaddowed. Do not proceed.')
% Get base workspace variable info
baseVarInfo = evalin('base', 'whos');
% Convert to table
rowNames = {};
baseVarInfo = rmfield(baseVarInfo,{'name','nesting','global','sparse','complex','persistent'});
baseVarCell = [num2cell(false(size(baseVarInfo))),struct2cell(baseVarInfo)']; % selection column
varNames = [{'Select'}; fieldnames(baseVarInfo)];
baseVarTable = cell2table(baseVarCell, 'RowNames', rowNames, 'VariableNames', varNames);
Load the table into your UITable
This can be embedded in your App or it can exist in a separate UIFigure.
fig = uifigure();
uit = uitable(fig, 'position', [20 20 500, 300],...
'ColumnEditable', [true, false(1,numel(varNames)-1)], 'Tag', 'BaseWorkspaceVarInfo');
uit.Data = baseVarTable;
Load the selected variables into your app
Check the checkboxes of variables you want to load into the app. Something needs to signal that the selection is complete and that a function should load the variables into the app. It could be a button in the app, or, if the UITable is in a separate figure, closing the figure could evoke this action. The code below will work in any case.
% Find the handle to the UITable (note the tag name assigned to the UITable)
% This step is avoidable if the UITable handle is already stored in app.
uitab = findall(0, 'Tag', 'BaseWorkspaceVarInfo');
% Get the user's selection and extract the variable names (varsToLoad).
selection = uitab.Data.Select;
varsToLoad = uitab.RowName(selection);
% loop through each variable and store it's values in a cell array.
varValues = cell(size(varsToLoad));
for i = 1:numel(varsToLoad)
varValues{i} = evalin('base', varsToLoad{i});
% The variable name is stored in varsToLoad{j}
% The variable value is stored in varValues{j}
Athrey Ranjith Krishnanunni
Hi Adam, there seems to be a problem with my communication preferences, because I never got to know that you had replied 😅. Apologies for the delay.
I had indeed tested it as you suggested, and got the same result. However, what I wanted was a way to ensure that the user can read their variables from the base workspace into my app even when the functions who, whos, which, exist, etc. are shadowed in their base workspace (like in Curve Fitting Toolbox).
I was able to achieve what I wanted by using
varNames = evalin('base','builtin(''who'')');
Now, if the user tries to shadow/ overload builtin before running my app, MATLAB itself will display a warning, so they know not to expect anything to work properly from that point onwards.
Thanks for your comments! 😊

Sign in to comment.


Find more on Develop Apps Using App Designer in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!