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)
Afficher commentaires plus anciens
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
- Using the evalin and assignin functions
- 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
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.
Réponses (1)
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:
- Using the Model Workspace: https://www.mathworks.com/help/releases/R2024a/simulink/ug/using-model-workspaces.html
- Editing data in the Model Workspace: https://www.mathworks.com/help/releases/R2024a/simulink/ug/change-model-workspace-data.html
- 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
Voir également
Catégories
En savoir plus sur Simulink Real-Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!