Shared nested structure between models in generated code

조회 수: 2 (최근 30일)
Mikel Manzanal Herrero
Mikel Manzanal Herrero 2023년 5월 24일
답변: Githin George 2023년 10월 13일
I have several models from which I generate C code using the Embedded Coder toolbox.
In these models, I would like to use a neseted structure variable to share information.
st1 = struct(...
'Fault_value', single(0),...
'Enable', boolean(0));
Faults = struct( ...
'model1', struct(...
'f1', st1,...
'f2', st1), ...
'model2', struct(...
'f1', st1,...
'f2', st1));
As this structure is shared across multiple models, the definition should be done in a separete file.
First I create a simulink bus for each strcuture:
faults_t = slBus3;
model1_t = model1;
model2_t = model2;
st1_t = f1;
clear model1 model2 f1 f2 slBus1_f1 slBus2_f2 slBus3
Then I assign the bus type to each element:
faults_t.Elements(1).DataType = 'Bus: model1_t';
faults_t.Elements(2).DataType = 'Bus: model1_t';
model1_t.Elements(1).DataType = 'Bus: st1_t';
faults_t.Elements(2).DataType = 'Bus: st1_t';
faults_t.Elements(1).DataType = 'Bus: st1_t';
model2_t.Elements(2).DataType = 'Bus: st1_t';
In order for the type to be defined outside the models headers I define each bus DataScope as Exported.
faults_t.DataScope = 'Exported';
model1_t.DataScope = 'Exported';
model2_t.DataScope = 'Exported';
st1_t.DataScope = 'Exported';
Finally I create a Simulink.Parameter of Faults and set the DataType to 'faults_t'
Faults = Simulink.Parameter(Faults);
Faults.CoderInfo.StorageClass = 'ImportedExternPointer';
Faults.DataType = 'Bus: faults_t';
One I have done this I set constant blocks inside the models using this names
  • Faults.model1.f1
  • Faults.model2.f2
The expected output would be:
  • The code is generated correctly
  • The models use a pointer to Faults variable
  • The definition of each structure is generated in a separated file. (test_model.c, test_model.h, faults_t.h, st1_t.h, ...)

답변 (1개)

Githin George
Githin George 2023년 10월 13일
Hi Mikel,
I understand you are trying to create a ‘Simulink Bus Object’ for ‘Faults’ and is facing an error in the model. I am assuming the error is caused due to wrong assignment of ‘DataType’ for the various ‘Bus Elements’. Please recheck code section 3 where the ‘Bus Elements’ are getting assigned. This should resolve the error in the model and allow you to generate code correctly. I hope this helps.




Community Treasure Hunt

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

Start Hunting!

Translated by