Matlab System Codegen error in Simulink

조회 수: 8(최근 30일)
Christopher Dadswell
Christopher Dadswell 2018년 5월 30일
Hi All,
Having an issue implementing a Matlab System in a Simulink model. The frameBufferSys system is intended to act as a moving average that operates on the frames of a video, code attached below. It takes 2 tunable parameters in the mask, the length of the moving average (number of frames it acts on), and the size of those frames. It works using interpreted execution, but with codegen I get the following error:
Simulink detected an error
'Dimension 1 is fixed on the left-hand side but varies on the right ([720 x 1280] ~= [:? x :?]).'.
The error occurred for MATLAB System block 'Moving_Average/MATLAB System'.
To prevent this error, use one of the following:
* Modify the System object to avoid code that does not support code generation.
* Change 'Simulate using' parameter to 'Interpreted Execution'.
The size of the inputs and outputs will not vary at all during simulation and should be set by the tunable parameter 'Resolution' in the mask. Any ideas how to fix this? Alternatively if anyone has a way to implement this with existing Simulink blocks that'd be great.
Many thanks, Chris
classdef frameBufferSys < matlab.System & matlab.system.mixin.Propagates ...
& matlab.system.mixin.CustomIcon
% frameBufferSys - Simulink system object for defining frame buffer
% objects to store image frames for processing, and outputting an
% averaged frame
% NOTE: When renaming the class name Untitled2, the file name
% and constructor name must be updated to use the class name.
% This template includes most, but not all, possible properties, attributes,
% and methods that you can implement for a System object in Simulink.
% Public, tunable properties
% nFB - Number of Buffer Frames
nFB = 9;
% Resolution - Frame Resolution
Resolution = [1280, 720];
% Public, non-tunable properties
% Pre-computed constants
properties(Access = private)
% Constructor
function obj = frameBufferSys(varargin)
% Support name-value pair arguments when constructing object
% setProperties(obj,nargin,varargin{:})
methods(Access = protected)
%%Common functions
function setupImpl(obj)
% Perform one-time calculations, such as computing constants
% Allocate memory for the frame buffer and average frame
obj.frames = zeros(obj.Resolution(2), ...
obj.Resolution(1), obj.nFB);
function framesAvg = stepImpl(obj, frame)
% Implement algorithm. Calculate y as a function of input u and
% discrete states.
% Adds frame to the buffer, shifting previous frames back one
% timestep and deleting oldest frame.
% Shift frames back one
obj.frames(:,:,1:end - 1) = obj.frames(:,:,2:end);
% Overwrite last frame with new frame
obj.frames(:,:,end) = frame;
% Average frames in the buffer over the length of it
framesAvg = sum(obj.frames, 3) / obj.nFB;
function resetImpl(obj)
% Initialize / reset discrete-state properties
% Reset frames to zero
obj.frames = zeros(obj.Resolution(2), ...
obj.Resolution(1), obj.nFB);
%%Backup/restore functions
function s = saveObjectImpl(obj)
% Set properties in structure s to values in object obj
% Set public properties and states
s = saveObjectImpl@matlab.System(obj);
% Set private and protected properties
%s.myproperty = obj.myproperty;
function loadObjectImpl(obj,s,wasLocked)
% Set properties in object obj to values in structure s
% Set private and protected properties
% obj.myproperty = s.myproperty;
% Set public properties and states
%%Simulink functions
function ds = getDiscreteStateImpl(obj)
% Return structure of properties with DiscreteState attribute
ds = struct([]);
function flag = isInputSizeMutableImpl(obj,index)
% Return false if input size cannot change
% between calls to the System object
flag = false;
function out = getOutputSizeImpl(obj)
% Return size for each output port
out = [obj.Resolution(2), obj.Resolution(1)];
% Example: inherit size from first input port
% out = propagatedInputSize(obj,1);
function icon = getIconImpl(obj)
% Define icon for System block
icon = mfilename("class"); % Use class name
% icon = "My System"; % Example: text icon
% icon = ["My","System"]; % Example: multi-line text icon
% icon = matlab.system.display.Icon("myicon.jpg"); % Example: image file icon
methods(Static, Access = protected)
%%Simulink customization functions
function header = getHeaderImpl
% Define header panel for System block dialog
header = matlab.system.display.Header('frameBufferSys', ...
'Title', 'Frame Buffer System Object');
function group = getPropertyGroupsImpl
% Define property section(s) for System block dialog
group = matlab.system.display.SectionGroup( ...
'Title', 'Buffer Properties', ...
'PropertyList', {'nFB', 'Resolution'});
function in1name = getInputNamesImpl
in1name = 'New Frame';
function out1name = getOutputNamesImpl
out1name = 'Average Frame';



Find more on Create 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!

Translated by