#Define Preprocessor statements in Simulink Library File

7 views (last 30 days)
After reading this mathworks support article, I would like to apply this to a set of Simulink Libraries:
However, it seems that libraries do not have options that this article points to (at least I cannot find them). Is there a way in the model explorer to define a custom type that creates a #define statement with a constant that generates to C code with a preprocessor #define statement?
DKalale on 15 Feb 2018
You are correct and your link was very helpful. When I go to the Model Explorer and look at my library directly under "Simulink Root" I can add data which I can then set as constants. However, I cannot add "data" to a subsystem block in my library (and so I cannot add a constant to those)
However, I have a library with various subsystems that I use as functions that I want to add preprocessor defined constants to (i.e. like the square root of 2 -- I'll call it kRootTwo). Right now when I define a constant in my library's model explorer content's view, I create the kRootTwo data there, but when I go to call my function in another simulink model (and project) the kRootTwo variable does not appear defined in the contents of that simulink model. So I get an error.
Do you know of any way to make the constant that I am defining be recognized in both the library and the model which has the library function? I think this means that I need the constant defined locally with the function in my library.

Sign in to comment.

Answers (1)

Chetna Jain
Chetna Jain on 16 Feb 2018
Try using "asignin" method to get the desired functionality.
In the InitFcn Callback of the library use "assignin(ws, 'var', val)".
This assigns the value val to the variable var in the workspace ws (here , ws = 'base')
This will enable the model which is using Subsytems from your library to view the data.
If the constant parameters are specific to a particular Subsystem in your library, you may instead use "assignin" function in the
Susbsystem's InitFcn Callback.
In this case, instead of putting var in model workspace, keep it in Subsytems InitFnc Callback.
For example, the Subsystem InitFcn would look something like :
kRootTwo = Simulink.Parameter
kRootTwo.Value = 1.414
assignin('base','kRootTwo', kRootTwo);
To understand "assignin" function , below mentioned is the link :
Hope this helps!
  1 Comment
DKalale on 17 Feb 2018
This is almost exactly what I need. I ended up putting the following code in the Init Callback:
kRootTwo = mpt.Parameter;
kRootTwo.Value = 1.4142135623730950488016887242097;
kRootTwo.DataType = 'single';
kRootTwo.RTWInfo.StorageClass = 'Custom';
kRootTwo.RTWInfo.Alias = '';
kRootTwo.RTWInfo.CustomStorageClass = 'Define';
assignin('base','kRootTwo', kRootTwo);
So I have a Parent "Model" and I put this code in the Init Callback of the Child "Model" (which is where I want the #define statment to be). Using the above code does the following:
Properly uses the constant in C code in the child model's *.c file:
And puts a #define statement in the Parent model's *.h file.
Now, this works ok because the autogenerated code automatically includes the "Parent.h" header file. However, is there a way to get the constant to be defined in the "Child.h" header file or C file?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!