Here I have a solution. It is not very pretty, but it works.
I can execute this code elswhere and create a mat file with the needed data structures:
MyDataStruct = Simulink.Bus.createMATLABStruct('MyDataType');
save('MyDataInit.mat', MyDataStruct, '-mat');
Once I have the mat-file, the following code in Matlab system object works both from Matlab and from Simulink as well as codegens to C and a standalone executable:
classdef MySysObj < matlab.System
properties (Access=private)
OutInitArgs
end
methods(Access = protected)
function setupImpl(this)
if coder.target('MATLAB')
initArgs = load('MyDataInit.mat','-mat');
else
initArgs = coder.load('MyDataInit.mat','-mat');
end
this.OutInitArgs = initArgs;
end
function InitializedData = stepImpl(this)
InitializedData = this.OutInitArgs.MyDataStruct;
InitializedData.fieldOfInterest = rand;
end
end
end
The only negative issue here is that I have to manage and keep track of the mat file, which is annoying, but other than that, it seems to work quite well.