Change Workspace of PLECS when called inside a MATLAB function

Dear all,

I want to call a Simulink model with PLECS inside a function. I was able to set the workspace for the Simulink model to the local-function workspace. However, i can not change it for PLECS. PLECS still need the variables to be located in the ‘base’ workspace in order to use them for initialization.

Has someone found a solution for this?

Kind regards Jan

This is described in the PLECS manual in the sections Using Workspace Variables in Parameter Expressions (page 51) and Customizing the Circuit Block (page 47, pages refer to the 4.3 edition of the manual). In short, you need to edit the Simulink mask of the Circuit block and define your desired workspace variables as mask parameters.

This is demonstrated in the attached model and m-file. Choose Mask > Edit Mask… from the context menu of the Circuit block and open the Parameters & Dialog tab to see how an Inductance parameter was added. Choose Mask > Mask Parameters… to open the dialog to edit the mask parameters.

Notice that the model uses the PreLoadFcn callback to define a default value for the inductance variable varL in the base workspace. The m-file is a function that defines a local variable varL in the function workspace and uses the sim command to run simulations in the function workspace.

buck_converter_with_parameter_sweep_ws.mdl (37.4 KB)

parameter_sweep_ws.m (1.07 KB)

Thanks for this nice example. I saw the description in the manual, but couldn’t make it work. With your example I saw my mistake. I added the parameter on the wrong hierachy level inside the Edit Mask window.

Thanks.

Thanks, Wolfgang. This is super useful. I wasn’t sure what I was supposed to do based on the documentation (pg 72 and 54 didn’t help too much, especially with the new layout of Simulink), but I was able to figure out how to script things by adding the parameters to the mask looking through the example.