Alternative to Simulink.Bus.createMATLABStruct call in Matlab System object
Show older comments
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
Answers (1)
Nikita Visnevski
on 28 Jan 2019
Categories
Find more on Implement Blocks with System Objects 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!