Errors using codegen when using structure as return values of function

2 views (last 30 days)
Hi folks,
I'm with Northrop Grumman.
Here's a function with a structure for input values (PhyInit), and a return value that I intend to be a structure:
I have a Test Bench that calls this code and it runs successfully. But when I compile with codegen, I get an error:
This structure does not have a field 'Waveform'; new fields cannot be added when structure has been read or used.
This structure does not have a field 'decDelay'; new fields cannot be added when structure has been read or used.
function [TxReturn] = PhyTxMlx(PhyInit) %#codegen
dataIn = PhyInit.data;
% Initialization variables are in PhyInit structure
% tPoly = poly2trellis(PhyInit.constrlen,PhyInit.genpoly); % codegen
tPoly = poly2trellis([5 4],[23 35 0; 0 5 13]);
dataEnc = convenc(dataIn,tPoly);
dataSymbolsIn = bit2int(dataEnc,PhyInit.BitsPerSymbol);
IQMod = qammod(dataSymbolsIn,PhyInit.M);
% TxReturn.rrcFilter =
% rcosdesign(PhyInit.rolloff,PhyInit.filtlen,PhyInit.sps); codegen
% constants
TxReturn.rrcFilter = rcosdesign(0.25,10,4); % codegen needs constants
txSignal = upfirdn(IQMod,TxReturn.rrcFilter,4,1);
%
% Return Values from this function
%
% TxReturn = struct; % create structure for return values
TxReturn.Waveform = txSignal; % return IQ complex waveform
TxReturn.decDelay = 10; % return filter delay for BER report
end
  1 Comment
Bill Chou
Bill Chou on 23 Jun 2023
Hello Dr. Kurt,
I'm with Product Marketing for Coder products including MATLAB Coder. I'd like to make sure we're able to help answer your questions in this thread and the other one you recently posted.
Could you reach out to me via matlab-coder-expert@mathworks.com? I can check with our Northrop Grumman account team to make sure we're able to help you along.
Thanks!
Bill Chou
Product Marketing, Code Generation

Sign in to comment.

Answers (1)

Ryan Livingston
Ryan Livingston on 23 Jun 2023
The issue that Coder is flagging is that you have something like
someStruct.field1 = value;
use(someStruct.field1);
% This line is what Coder is complaining about because someStruct was used
% above
someStruct.field2 = value;
In particular, for your code I'd suggest rewriting to
function [TxReturn] = PhyTxMlx(PhyInit) %#codegen
dataIn = PhyInit.data;
% Initialization variables are in PhyInit structure
% tPoly = poly2trellis(PhyInit.constrlen,PhyInit.genpoly); % codegen
tPoly = poly2trellis([5 4],[23 35 0; 0 5 13]);
dataEnc = convenc(dataIn,tPoly);
dataSymbolsIn = bit2int(dataEnc,PhyInit.BitsPerSymbol);
IQMod = qammod(dataSymbolsIn,PhyInit.M);
% TxReturn.rrcFilter =
% rcosdesign(PhyInit.rolloff,PhyInit.filtlen,PhyInit.sps); codegen
% constants
% Ryan: Don't write into the struct and then read it here to avoid
% Coder limitations. Use a temporary rrcFilter in the interim.
rrcFilter = rcosdesign(0.25,10,4); % codegen needs constants
txSignal = upfirdn(IQMod,rrcFilter,4,1);
%
% Return Values from this function
%
% TxReturn = struct; % create structure for return values
% Ryan: Now write the values into the output struct
TxReturn.rrcFilter = rrcFilter;
TxReturn.Waveform = txSignal; % return IQ complex waveform
TxReturn.decDelay = 10; % return filter delay for BER report
end
  2 Comments
Dr W Kurt
Dr W Kurt on 23 Jun 2023
Ryan,
Thank you so much. What I did was set it up such that the structure is created all at once:
f1 = 'EIRP'; v1 = EIRP;
f2 = 'decDelay'; v2 = decDelay;
f3 = 'dataOut'; v3 = dataOut;
f4 = 'traceBack'; v4 = traceBack;
TxAntennaReturn = struct(f1,v1,f2,v2,f3,v3,f4,v4);
This works, and it also makes the code much easier to read/interpret

Sign in to comment.

Categories

Find more on Applications in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!