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

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!

 Respuesta aceptada

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 comentarios

Thanks, a bunch for your response!
Using the Simulink logic blocks, I can generate an adjusted value of the switching frequency, fsw based on the value of Idc while the simulation is running.
However, I am using the fsw for a PWM Generator (Three Phase, two-level) which accepts fsw as block parameter. There is no option I can feed fsw with an Inport to the PWM Generator block. Now, how can use the Idc-adjusted fsw for that PWM Generator block?
Pleas provide a link to the doc page for the PWM Generator block.
https://www.mathworks.com/help/sps/ref/pwmgeneratorthreephasetwolevel.html
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.
Thanks, a bunch for your suggestions and effort to help me!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Preguntada:

el 21 de Nov. de 2023

Comentada:

el 22 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by