App Designer-Unrecognized variables

4 Ansichten (letzte 30 Tage)
PanPan
PanPan am 17 Okt. 2024
Bearbeitet: Bruno Luong am 17 Okt. 2024
Hello all,
I am using app designer to build a simulaiton app. I have created a button which is supposed to run a script which then simulates a simulink model and plots some graphs.
In the app, the user assigns variables to the workspace via edit fields. However, even though the required variables for the script to run are assigned in the base workspace, I get an error of unrecognized variables.
What could be the issue here?
Thank you in advance.
Here's the code for the function:
% Button pushed function: RUNButton
function RunSimu(app, event)
% Get all variable names in the base workspace
vars = evalin('base', 'who');
% Loop through each variable and assign it in the function's workspace
for i = 1:length(vars)
varName = vars{i};
varValue = evalin('base', varName);
% Assign the variable to the function workspace
assignin('caller', varName, varValue);
end
run('APPCODE_FULLSIMULATION');
end
  2 Kommentare
Sameer
Sameer am 17 Okt. 2024
Using assignin('caller', ...) does not assign variables to the local function workspace. Instead, it assigns them to the workspace of the function that called your current function. In this case, it might not be doing what you expect. You might want to use assignin('base', ...) if you need to ensure they are in the base workspace.
PanPan
PanPan am 17 Okt. 2024
Hello @Sameer,
I tried what you proposed and I still get the same error. As it seems, I am a bit confused with the local workspaces. Any suggestion on how to procced and make it work ?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 17 Okt. 2024
Bearbeitet: Bruno Luong am 17 Okt. 2024
Try this:
% Button pushed function: RUNButton
function RunSimu(app, event)
% Get all variable names in the base workspace
vars = evalin('base', 'who');
% Loop through each variable and assign it in the function's workspace
assignfun = @(varName, varValue)assignin('caller', varName, varValue);
for i = 1:length(vars)
varName = vars{i};
varValue = evalin('base', varName);
% Assign the variable to the function workspace
assignfun(varName, varValue);
end
run('APPCODE_FULLSIMULATION');
end
Though this is a bad programmation style with assihnin and evalin. You need to learn passing variables diffrently, using function arguments.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by