Continuously Update MATLAB Variable and Use in Simulink

110 vues (au cours des 30 derniers jours)
Leon
Leon le 2 Déc 2022
I was trying to use Simulink Desktop Real-Time in order to implement a real-time communication simulation with ModBus Protocol, where I send data (a request) from qModMaster (a tool, which imitates Master PC) to MATLAB using tcpserver. Then I instantly assign the data to a variable {var_name} and use the variable in Simulink in combination with Real-Time Sync Block. However, when a simulation starts, it takes the initial value of the {var_name}, and when I send another data(request), the {var_name} updates in the base workspace, but not in the Simulation. Is there any solution without using C/C++ S-Functions?

Réponse acceptée

Jan Houska
Jan Houska le 8 Déc 2022
Hi Leon,
please do set_param(gcs, 'SimulationCommand', 'update') after you change the value of the variable in the base workspace. This should update the model with new values of any workspace variables.
Good Luck, Jan

Plus de réponses (1)

Suchitra
Suchitra le 6 Déc 2022
Hello Leon,
I understand that you want to update the workspace variables during a simulink simulation and have the new values used while the simulation is running.
There are several ways to achieve it, depending on your use case:
1. Saving and restoring the simulation state as the 'SimState':
The Simulink 'SimState' feature allows you to save all run-time data necessary for restoring the simulation state of a model. A SimState includes both the logged and internal state of every block (e.g., continuous states, discrete states, work vectors, zero-crossing states) and the internal state of the Simulink engine (e.g., the data of the ODE solver).
2. Tunable Block Parameters:
Simulink lets you change the values of many block parameters during simulation. Such parameters are called tunable parameters. You can tell whether a particular parameter is tunable by examining its edit control in the block's dialog box or Model Explorer during simulation. You can also tune a parameter at the MATLAB command line, using either the set_param command or by assigning a new value to the MATLAB workspace variable that specifies the parameter's value.
Refer to the following documentation link to learn more about usage of 'Tunable Parameters':
3. Simulink.Parameter:
This class enables you to create workspace objects that you can then use as the values of block parameters, (for example, the value of the Gain parameter of a Gain block). You can create a Simulink.Parameter object in the base MATLAB workspace or a model workspace.
Refer to the following documentation link to learn more about 'Simulink.Parameter':
If a block parameter needs to be evaluated (and changed if needed) at every simulation time step, a programmatic way of stepping through the simulation is to use the following command in a loop:
set_param(modelName, 'SimulationCommand', 'step');
4. You can also employ event listeners to make parameter modifications only when certain events happened. Below is an example:
I think this should resolve the issue you are facing. If doing the above does not help prevent the issue, please contact Technical Support
MathWorks Support information is available here:

Community Treasure Hunt

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

Start Hunting!

Translated by