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.

Creating blocks in Plecs with scripts

0 votes
222 views
Is it possible to create blocks in Plecs from a script?

Previously, in Simulink I have created a custom library with masked S-function blocks and saved this as a library all from the same Matlab script. I was wondering if there was a similar way to do this in Plecs, in this case just masking the C-script blocks?
asked Aug 19 by Kristian Pedersen (12 points)

1 Answer

0 votes

Your question and desired workflow isn't 100% clear to me.  My interpretation: "Is there a way to create a custom mask that encapsulates a C-Script block?" to which the answer is yes.  Please see the "PLECS: Creating Custom Components and Subsystem Masks" tutorial. One can pass parameters defined in the component mask into the C-Script using the C-Script's parameter field.

Please let me know if I misunderstood your question.  If so, please provide a more detailed explanation, graphic, or psuedocode describing your goals.

answered Aug 21 by Bryan Lieblick (2,065 points)
I have already achieved what you are describing with creating a custom mask to encapsulate a C-script block. However, I have done this manually in a Plecs model, where I have dragged the C-script and in- and out ports from the Plecs library and manually connected them in my model.

What I would like to do, is to do this from a script, so I can create multiple of these masked subsystems by running this script. I have previously achieved this in Matlab/Simulink. A short snippet of the code:

--------------------------------

% Add the block with the relevant parameters

add_block('simulink/User-Defined Functions/S-Function',lib_name + "/" + blockName,'FunctionName',block_sWrapper,'Parameters',parameters_mask);

maskObj = Simulink.Mask.create(gcb);

set_param(gcb,'MaskStyleString',edit_str,...

'MaskVariables',parameters_order,...

'MaskPromptString',parameters_text,...

'MaskValueString',parameters_value,...

'MaskDisplay',"image(" + "'C:\MATLAB\R2023b\EltwinMClib\Mask_icons\" + blockName  + ".png')")   

---------------------------------------

As you can see from the code, Matlab allows me to create a block from a script, create a custom mask for it, and set the parameters of the mask all from within a script. This it what I would hopefully like to do in Plecs as well.

Thanks for clarifying Kristian - it helped me better understand your goals.

There isn't a similar script-based interface to add blocks to a schematic.  However, once the block is added to the schematic, the C-Script fields are all configurable as parameters.  

For example:

% Set command convention: plecs('set', 'componentPath', 'parameter', 'value')
DeclarationString = "#include <math.h>\n";
plecs('set','./PathToScript/C-Script','Declarations',DeclarationString)

For many components in PLECS, you can determine the parameter name by clicking the check-box near the parameter entry or in the component mask editor window. The parameter name for each field in a C-Script may not be obvious (e.g. 'Declarations', 'StartFcn', 'OutputFcn', etc.). PLECS Standalone files are plain-text.  By reviewing the portion of the model describing the C-Script the various parameter names will be apparent.

Since the PLECS Standalone file is plain text, one can also programatically construct a model by writing the appropriate text strings, which I have done in the past.  Note that the file format is subject to change in future versions of PLECS.

...