- Using a wrapper class: Write your own class that contains the generated model class as a member, and add your private variable there.
 - Automating your changes via a script if frequent regeneration is expected.
 
Is there any way to create private member variables of a custom data type within autogenerated c++ code from Simulink?
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
I am trying to create a custom data type (imported from an external header file) as a private member variable in an autogenerated C++ class from a simulink model.  I am using a C-function to define functionality that uses this custom data type, but I want to be able to access the same variable within multiple functions (hence making it a member variable instead of a local variable).  I would also like to prevent any access outside the class, which is why I want to specify it as a private member variable.  
This custom code will have no impact on the simulink simulation, it is exclusively used to integrate the autogenerated code with handwritten code (So in my C-functions, the simulation portion is completely empty). 
0 comentarios
Respuestas (1)
  Anushka
 el 5 de Mayo de 2025
        To use a custom data type as a private member variable in the C++ class generated by Simulink, you first need to define that custom type in your own code—typically in an external header file (for example, "my_custom_type.h"). This is necessary because Simulink’s code generator only knows about data types defined in the model or included via custom code settings, and it cannot create or infer new complex types on its own.
Once your custom data type is defined and available to the code generator, you can then integrate it into the generated class as a private member variable. Here’s how you can do this:
1. Open your model, go to Model Configuration Parameters > Code Generation > Custom Code. Under 'Include headers', add:
#include "my_custom_type.h"
If your header isn’t in the model’s folder, add its path under 'Include directories'.
2. After you generate code (e.g., "MyModel.h"), you will see the auto-generated class. You can add your custom variable as a private member like this:
private:
MyCustomType myVar_;
3. Suppose you have a function in your own C++ file, like:
void updateCustomData(MyCustomType* obj);
In the model’s source file (e.g., "MyModel.cpp"), you can include your logic and use the variable:
#include "my_custom_logic.h"
void MyModel::step() {
updateCustomData(&myVar_);
}
This way, your custom type is used only in your code and it will not affect the Simulink simulation at all.
Any manual edits to the auto-generated files (like adding a private member variable) will be overwritten when you regenerate code.
To avoid this, you might consider:
Hope it helps!
0 comentarios
Ver también
Categorías
				Más información sobre Simulink Coder 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!