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

4 vues (au cours des 30 derniers jours)
Nikita Visnevski
Nikita Visnevski le 17 Jan 2019
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

Réponses (1)

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

Catégories

En savoir plus sur Create System Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by