#Define Preprocessor statements in Simulink Library File

2 visualizaciones (últimos 30 días)
DKalale
DKalale el 7 de Feb. de 2018
Comentada: DKalale el 17 de Feb. de 2018
After reading this mathworks support article, I would like to apply this to a set of Simulink Libraries:
https://www.mathworks.com/matlabcentral/answers/184184-how-do-i-make-sure-simulink-constants-are-coded-as-define
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?
  4 comentarios
Chetna Jain
Chetna Jain el 13 de Feb. de 2018
Hi,
I am unable to understand what “options ” are you referring to here.  Perhaps on left most Model Hierarchy panel  the “base workspace” is not selected and thus the options might be greyed out in Model Explorer.
Below mentioned link can be a good help if you still face issues or for further reference : https://www.mathworks.com/help/ecoder/ug/macro-definitions-define.html#bsi4as4-1
DKalale
DKalale el 15 de Feb. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Chetna Jain
Chetna Jain el 16 de Feb. de 2018
Hi,
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 comentario
DKalale
DKalale el 17 de Feb. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by