Matlab System Codegen error in Simulink
2 次查看(过去 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
properties
% nFB - Number of Buffer Frames
nFB = 9;
% Resolution - Frame Resolution
Resolution = [1280, 720];
end
% Public, non-tunable properties
properties(Nontunable)
end
properties(DiscreteState)
frames
end
% Pre-computed constants
properties(Access = private)
end
methods
% Constructor
function obj = frameBufferSys(varargin)
% Support name-value pair arguments when constructing object
% setProperties(obj,nargin,varargin{:})
end
end
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);
end
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;
end
function resetImpl(obj)
% Initialize / reset discrete-state properties
%
% Reset frames to zero
obj.frames = zeros(obj.Resolution(2), ...
obj.Resolution(1), obj.nFB);
end
%%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;
end
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
loadObjectImpl@matlab.System(obj,s,wasLocked);
end
%%Simulink functions
function ds = getDiscreteStateImpl(obj)
% Return structure of properties with DiscreteState attribute
ds = struct([]);
end
function flag = isInputSizeMutableImpl(obj,index)
% Return false if input size cannot change
% between calls to the System object
flag = false;
end
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);
end
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
end
end
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');
end
function group = getPropertyGroupsImpl
% Define property section(s) for System block dialog
group = matlab.system.display.SectionGroup( ...
'Title', 'Buffer Properties', ...
'PropertyList', {'nFB', 'Resolution'});
end
function in1name = getInputNamesImpl
in1name = 'New Frame';
end
function out1name = getOutputNamesImpl
out1name = 'Average Frame';
end
end
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Create System Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!