Clear Filters
Clear Filters

Simulink model variables are not updated when set using setVariable in Rapid Accelerator and compiled mode.

49 views (last 30 days)
I am trying to run a Simulink model from MATLAB. The end product is a bit of C++ code calling a compiled DLL (shared library) obtained through MATLAB Compiler and SImulink Compiler, so I cannot use the function set_param, for example.
I'm following this workflow:
or, to be more precise, this one, which assigns the value to the model workspace:
However, my variables are not updated, whether from MATLAB directly or when compiled into a DLL. I can see that the MATLAB function calling the model is getting the right values, however the model workspace doesn't seem to get the memo.
It probably has something to do with the models running in Rapid Accelerator mode, for the sake of being compilable into a shared library. The values that displayed from the SL model are probably the ones that have been saved in cache.
I tried using the applyToModel method on my SimulationInput object, however, while this seems to work in MATLAB/Simulink, I get an error message when calling the model from my C++ code, therefore I'm assuming that it is not compatible with Simulink Compiler. The error message is: "One or more output arguments not assigned during call to "varargout"" and the stack trace goes into applyToModel.
Any suggestions would be appreciated - I tried many things so far, to no avail. I'm afraid I cannot share my files due to IP reasons.
  1 Comment
Pierre-Augustin
Pierre-Augustin on 14 Mar 2023
I will elaborate with more elements that I gathered from the documentation:
This thread is telling me that if there is a conflict between a variable from the Model workspace and a variable in a SimulationInput instance, it needs to be dealt with:
However, the variable has to exist in the Model workspace to be used (and modified) as part of the simulation, including in a compiled model, see this example, which explicitly states "External input variables must be in the MATLAB workspace before packaging for deployment." :
From now on, I fail to understand the correct way modify a variable in the model workspace. On top of this, there are limitations around rapid accelerated models, which is the case for me, since I am trying to compile a model into a shared library. These limitations are the following:
"If a block parameter value references workspace variables, you cannot change the block parameter value during rapid accelerator simulation, such as by using the function set_param. Instead, you can tune the values of the referenced variables."
set_param is not compatible with MATLAB Compiler, which is why I resort to SimulationInput.setVariable. But I do not understand what I am doing wrong here, or what I am supposed to do differently. I am trying to change the values of my variables before launching the simulation with sim, not during the simulation.

Sign in to comment.

Answers (1)

Gayatri Rathod
Gayatri Rathod on 28 Apr 2023
Hi Pierre-Augustin,
It seems like you are facing several challenges in trying to modify the variables in the model workspace of your Simulink model. Here are some suggestions that may help:
  • Check the model's configuration settings: Make sure that the "Data Import/Export" settings for your model are set up correctly and that any variables you want to modify are listed under "Exported Global Data". This should ensure that your variables are accessible to your MATLAB code and can be modified as needed.
  • Using the SimulationInput object: You are on the right track with the SimulationInput object. This object is used to set the input values for a simulation. You can use the setVariable method to set the values of the variables in the model workspace. However, there are some things to keep in mind:
  1. The variable must exist in the model workspace before you can modify it using SimulationInput.setVariable().
  2. The values you set using SimulationInput.setVariable() will only be used during the simulation. They won't modify the actual values in the model workspace.
  • Try setting the variables directly in the model workspace: you can try setting the variables directly in the model workspace using the evalin function. For example, you can use the following code to set the value of a variable named "my_var" to 10:
evalin('base', 'my_var = 10;');
  • Modifying variables in a separate workspace: If you want to modify variables without modifying the values in the model workspace, you can create a separate workspace for your simulation. To do this, you can use the Simulink.SimulationInput.create method. This will create a new workspace that you can use to set the input values for your simulation.
You can read more about the setVariable and evalin functions from the following documentations: setVariable function, evalin function.
Hope it helps!

Categories

Find more on Run Individual Simulations 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!