Alternative to Simulink.B​us.createM​ATLABStruc​t call in Matlab System object

6 次查看(过去 30 天)
I learned the hard way that using Simulink.Bus implementations are not allowed in System objects or any other matlab function block. Are there any alternatives?
Here is what I need. I have a rather cumbersome and complicated data structure portions of which may change at any time in the future. I need to build a Simulink block which outputs this data structure on a bus partially initialized.
I have a script which defines a bus object and I want to instantiate a data structure inside a System object setupImpl method to then populate some fields of it with some values in stepImpl. Thsi is what I have tried:
classdef MySysObj < matlab.System
properties (Access=private)
OutArg
end
methods(Access = protected)
function setupImpl(this)
this.OutArg = Simulink.Bus.createMATLABStruct('MyDataType');
end
function InitializedData = stepImpl(this)
InitializedData = this.OutArg;
InitializedData.fieldOfInterest = rand;
end
end
end
'MyDataType' bus object may have a very complicated structure with hundreds of fields and it may change in the future, but 'fieldOfInterest' will always be there and it needs to be initialized to some value (this of course is a trivialization of what I am actually trying to do).
Of course Simulink complains when I am trying to run it:
Simulink detected an error 'Undefined function or variable 'Simulink.Bus'.'. The error occurred for MATLAB System block 'SystemTstHarness/Data Processors/MATLAB System'. See line 7, column 27 in file 'C:\Users\200013405\Documents\GitHub\DeploymentAcceleration\src\matlab\examples\MySysObj.m'. The error was detected during pre propagation phase. Start code generation report. To prevent this error, use one of the following: * Modify the System object to avoid code that does not support code generation. * Implement propagation methods in the System object code.
The only alternatives I found so far is to put Simulink.Bus.createMATLABStruct('MyDataType') call into Model init callback of the simulink block diagram, then assign the created dummy data structure to a constant block and feed its output into an input of a system object. It works, but is very ugly and cumbersome.
Any better alternatives???
Thanks

回答(1 个)

Nikita Visnevski
Nikita Visnevski 2019-1-28
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.

类别

Help CenterFile Exchange 中查找有关 Create System Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by