Updating simulink block parameter as the simulation runs.

Hello!
I'm new to Matlab and I'm wondering if this is possible.
I have a custom block with a mask that takes in 3 user-input parameters. One of the parameters is frequency and I want to vary the frequency with time while the simulation is running. I looked all over the internet and I found a suggestion to replace the frequency parameter with a workspace variable array, with the array setup as [time1 value1;time2 value2; time3 value3....]. I tried this, but I got an error saying that the there was an issue with the blocks in the subsystem reading the arrays properly. I placed this code into the InitFnc callback.
Am I doing going about this correctly? If so, what do I modify to ensure that the frequency input ports are correctly reading an array input?
If not, is there a different way I can go about changing the frequency with time? I am trying to do everything using a script, rather than including blocks like time steps since the block is masked.
Thanks!

 Réponse acceptée

setting array value as [time1 value1;time2 value2; time3 value3....] works for signal, not for parameters. Use this approach instead
set_param(YourModel, 'SimulationCommand', 'pause');
set_param(YourBlock, parameter, value);
set_param(YourModel, 'SimulationCommand', 'continue');

5 commentaires

Would those lines of code be placed under a callback function such as PauseFCN?
If the frequency needs to be changed several times during a simulation, it is better to make it a signal rather than a parameter. Then you can define F=[time1 value1;time2 value2; time3 value3....] in base workspace and use a "From Workspace" block in your model and specify the data as F.
But that will require you to change your masked subsystem, remove the mask parameter, change it to be an Inport and adjust the blocks inside.
If you can't do that, then here is an alternative.
In your model, add a Clock block and compare (>=) its output to a Constant block with value as time1. The comparision output feeds to an Assertion block, wihch specifies a callback PauseFCN.
When simulation runs to time1, the Assertion will trig the callback PauseFCN, in which you use the above code to
  1. Pause simulation
  2. Change the masked frequency parameter value
  3. Change the Constant block value from time1 to time2
  4. Continue the simulaiton
This way, you can add as many points as you want for the frequency changes. Somewhere in PauseFCN, you need to have some logic to Stop the simulation.
I'm trying to implement your second suggestion and I'm running into this error when running the simulation:
" Error evaluating 'AssertionFcn' callback of Assertion block 'filename/Assertion'. Callback string is 'PauseFcn', Undefined function or variable 'PauseFcn'. "
The error still shows up when I replace with other callback functions such as StopFcn, InitFcn, etc.
It has nothing to do with other functions. The Assertion block is calling PauseFcn, so you have to have a .m script called PauseFcn.m in your MATLAB path.
ohhh I see. I thought that part could link straight to the callback functions associated with the model. Thanks for your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by