Update M-script parameter value Continuously based on Workspace Data fed by a Simulink Model

3 views (last 30 days)
I am simulating a Simulink model for n seconds run from a Matlab script. In the Simulink model, I have a parameter that is being saved in the workspace. Now, based on the workspace data, I want to continuously update a parameter in m-file and feed the updated value in the Simulink model. After having an if else statement in the m-file, I see my target parameter is not being updated in the Simulink model.
For example, in my Simulink model, I have a DC bus current, Idc is being generated. I have the Idc stored in the workspace using the “To Workspace” block. Now, based on the magnitude of Idc, which is saved in the workspace, I want to update the switching frequency, fsw in m-script CONTINUOUSLY. Then, I want the the Simulink model, which is being simulated, should get the updated value of fsw continuously from the m-script.
For example, I tried this way but it does not help!
if Idc < 100
fsw = 100e3;
else
fsw = 200e3;
end
This code does not update the value of fsw continuously in the Simulink model. Any help would be appreciated!

Accepted Answer

Paul
Paul on 21 Nov 2023
The To Workspace block doesn't update the output in the workspace "continuously" while the simulation is running. Also, Simulink doesn't read data from the workspace "continuously" while the simulation is running, at least not in a reliable way.
The far superior approach is to implement all logic, algorithms, etc. in the Simulink model itself. If you have a .m function file on your path that you wish to execute from within a Simulink model, you can call tha function from inside a Matlab Function block. The input to the block would be Idc, the Matlab Function would call the .m function with Idc as an input, the .m function returns fsw as an output, and fsw then becomes the output of the Matlab Function block. Like any other block, you can set the Sample Time of the Matlab Function block. If code cannot be generated for your .m function, it will need to be declared using coder.extrinsic inside the Matlab Function.
Or, implement the function logic using Simulink blocks.
  5 Comments
Paul
Paul on 22 Nov 2023
Assuming you’re talking about the Switching Frequency parameter, it would appear that that parameter is not really intended to change during simulation or is perhaps intended to change infrequently. In that case this blog post may be of interest:
https://blogs.mathworks.com/simulink/2023/09/27/signals-vs-parameters-in-simulink-and-the-parameter-writer-block/
Don’t know if it will work for your application.

Sign in to comment.

More Answers (0)

Categories

Find more on General Applications 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!