Please take a minute to review and accept our Terms of Use.
Welcome to the PLECS User Forum, where you can ask questions and receive answers from other members of the community.

Many technical questions regarding PLECS are answered on the Technical Solutions page of our website. Tutorial videos, specific application examples, and pre-recorded webinars are available on our YouTube page. Please follow us on LinkedIn for the latest Plexim news.

Change Workspace of PLECS when called inside a MATLAB function

0 votes
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
asked Apr 21, 2020 by JanLafrenz (16 points)
edited Apr 22, 2020 by Wolfgang Hammer

1 Answer

+1 vote
Best answer

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.

answered Apr 22, 2020 by Wolfgang Hammer (401 points)
selected Apr 22, 2020 by JanLafrenz
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, 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.