Application Compiler: Why do I get the error "Variable 'input1' has been deleted from base workspace" and subsequently a failure to package the code in Application Compiler

8 vues (au cours des 30 derniers jours)
Hello Guys,
I have a Matlab file , which defines the variables in the Simulink model and then runs it. A Simulink.SimulationInput Object is used to this end.
The code i have used is a simple one.
simIn = Simulink.SimulationInput("Model");
simIn = simulink.compiler.configureForDeployment(simIn);
simIn = setModelParameter(simIn,RapidAcceleratorUpToDateCheck="on");
input1 = 1;
simIn = setVariable(simIn, "input1" , input1);
simOut = sim(simIn);
Running it in Matlab doesn't lead to any errors. However when i try to create an executable using Application Compiler, the following error message pops up
Unrecognized function or variable 'input1'.
Error using buildRacTarget
Variable 'input1' has been deleted from base workspace.
The only way for me to prevent this error is to define the variable in the command window beforehand i.e before running the Application Compiler to create the executable. However this is not a usefull solution for my purposes.
I have tried multiple approaches like
  1. Using the evalin and assignin functions
  2. Using a 2nd Matlab script called initial.m, which defines the variable input1 and then passes it into a .m (which is a function script with the definition function model(input1) that defines the Simulink.SimulationInput object and runs the simulink model. Once again, this works perfectly in Matlab and Simulink but cannot be compiled into an executable.
However, these approaches haven't worked.
My question is , how can I fix it? Why is the variable defined in the .m file being deleted? Why doesn't this problem occur when executed in Matlab and Simulink?
  3 commentaires
Sidharth
Sidharth le 20 Déc 2024
But how would i incorporate waitfor inorder to prevent the deletion of the object? Waitfor can only block the executation of a certain piece of code until a certain condition is true right?
Walter Roberson
Walter Roberson le 20 Déc 2024
For example, create a figure and waitfor() the figure. That forces the script to pause until the figure is deleted -- hopefully preserving the base workspace until the figure is deleted.

Connectez-vous pour commenter.

Réponses (1)

Aravind
Aravind le 26 Déc 2024
It sounds like you're facing an error with the "Application Compiler" in MATLAB, where the variable “input1” is not recognized during execution and compilation if it’s not already defined in the base workspace.
This issue arises because when Simulink tries to update the model, it cannot find the variable “input1”. When executed in MATLAB, the script works because the variable is defined in the base workspace before the Simulink model updates, allowing Simulink to access it. However, during compilation with the Application Compiler, the code is not executed, so “input1” is not set in the base workspace, causing the error.
To resolve this, you should define the variable “input1” with a default value, like 0, in the Model Workspace. The Model Workspace is specific to each Simulink model and is independent of MATLAB's base workspace. By setting “input1” in the Model Workspace, Simulink will always have a value for it when updating the model. You can then programmatically change the variable in the Model Workspace using the “setVariable” function like this:
simIn = setVariable(simIn, "input1", input1, "Workspace", mdl);
Here, "mdl" is the name of your model.
This approach should allow you to compile your MATLAB code into an executable without encountering the "Variable not found" error.
For more information on working with the Model Workspace, you can refer to these resources:
  1. Using the Model Workspace: https://www.mathworks.com/help/releases/R2024a/simulink/ug/using-model-workspaces.html
  2. Editing data in the Model Workspace: https://www.mathworks.com/help/releases/R2024a/simulink/ug/change-model-workspace-data.html
  3. Using the “setVariable” function: https://www.mathworks.com/help/releases/R2024a/simulink/slref/simulink.simulationinput.setvariable.html#bvoj5dn-1
I hope this helps resolve your issue.
  1 commentaire
Sidharth
Sidharth le 2 Jan 2025
Modifié(e) : Sidharth le 2 Jan 2025
Thank you for the answer!
Now, however I ran into a new Problem. The default value that I set for "input1" in the model workspace is not being replaced by the "SetVariable" function, despite using the same syntax you recommended.
The reason for this is that I configure the Simulation Input Object for deployment
simIn = simulink.compiler.configureForDeployment(simIn);
This is to prevent errors cropping up when i run the executable generated by the Application Compiler in Python. Placing this line before the "SetVariable" function or after it doesn't resolve the issue.
I also tried another set of commands involving the "assignin" funtion
input1 = 1;
assignin('base', 'input1',input1);
modelWorkspace = get_param(mdl, 'ModelWorkspace');
modelWorkspace.assignin('input1',input1);
% Define the SimulationInput object
simIn = Simulink.SimulationInput(mdl);
% Configure for deployment
simIn = simulink.compiler.configureForDeployment(simIn);
simIn = setModelParameter(simIn,RapidAcceleratorUpToDateCheck="off");
% Run and generate Output
simOut = sim(simIn);
This was successfull in being able to overwrite the predefined variables in the Model Workspace and subsequently in compiling an executable in the Application Compiler. However get_param is not supported in Python, so it's not possible to run this executable in a Python environment.
Is there another approach I could try?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Simulink Real-Time dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by