Matlab Coder stop removing extraneous data

3 views (last 30 days)
Eric Bickford
Eric Bickford on 13 Dec 2018
Answered: Balavignesh on 2 Jul 2024
Is there a way to stop matlab from removing the extraneous data structures for inputs to functions? Basically I am passing a struct with 20 feilds to a function but currently only using 5. In future code I may end up using the other 15 and I want the generated C code to input the full 20 feilds instead of removing the extraneous feilds and only having 5 inputs. I want to pass the entire struct and not the seperate 5 feilds in the struct.

Answers (1)

Balavignesh
Balavignesh on 2 Jul 2024
Hi Eric,
I understand that when using MATLAB Coder to generate C code, it often optimizes the code by removing unusual fields from structures. However, if you want to to ensure that all fields in a structure are preserved in the generated C code, you can use the coder.cstructname function to enforce the inclusion of all fields, even if they are not used in the current MATLAB code.
Below is a brief demonstation on how you can do it:
% Define the structure with 20 fields
structDef = struct('field1', 0, 'field2', 0, 'field3', 0, 'field4', 0, 'field5', 0, ...
'field6', 0, 'field7', 0, 'field8', 0, 'field9', 0, 'field10', 0, ...
'field11', 0, 'field12', 0, 'field13', 0, 'field14', 0, 'field15', 0, ...
'field16', 0, 'field17', 0, 'field18', 0, 'field19', 0, 'field20', 0);
% Function definition
function myFunction(structInput)
%#codegen
% Preserve all fields in the structure
coder.cstructname(structInput, 'MyStructType');
% Your function implementation
% For example, using only 5 fields currently
result = structInput.field1 + structInput.field2 + structInput.field3 + ...
structInput.field4 + structInput.field5;
end
Kindly have a look at the following documentation links to have more information on:
Hope that helps!
Balavignesh

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!