Matlab system block dimension: Dimension 1 is fixed on the left-hand side but varies on the right
7 views (last 30 days)
Show older comments
Benjamin Campbell
on 4 Nov 2022
Answered: Brandon Stevens
on 14 Nov 2022
I am trying to write a "Matlab system" to use as a block in simulink and get the following error:
Caused by: Simulink detected an error 'Dimension 1 is fixed on the left-hand side but varies on the right ([30 x 1] ~= [:? x 1]).'.
It works when I "Simulate using: Interpreted Execution". But, it doesnt work when I "Simulate using: Code generation".
I thought I had defined the sizes of everything and can't figure out how to solve the problem. Thank you very much for your help!
classdef implicit_adaptor < matlab.System
% implicit_adaptor should take in z and e, and output de/du that can be
% used for an adaptive filter for C2 (feedforward control)
% States: A (N x N) and old_z (N x 1)
% Outputs: de_dz (N x 1) and error (1 x 1)
properties
% Mu Step size
Mu = 0.005;
% N length of z
N = 30;
% Sample time
dt = 1/500;
end
properties (Nontunable)
end
properties (DiscreteState)
A;
old_z;
end
methods (Access = protected)
% Initialise states
function setupImpl(obj)
obj.A = zeros(obj.N);
obj.old_z = zeros(obj.N, 1);
end
% Set up output data
function [dataout1, dataout2] = getOutputDataTypeImpl(~)
dataout1 = 'double';
dataout2 = 'double';
end
function [cplxout1, cplxout2] = isOutputComplexImpl(~)
cplxout1 = false;
cplxout2 = false;
end
function [size_de_dz, size_error] = getOutputSizeImpl(obj)
size_de_dz = [obj.N 1];
size_error = [1 1];
end
function varargout = isOutputFixedSizeImpl(obj)
% Get outputs fixed size.
varargout = cell(1, getNumOutputs(obj));
for i = 1:getNumOutputs(obj)
varargout{i} = true;
end
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(obj, name)
if strcmp(name,'A')
sz = [obj.N obj.N];
dt = 'double';
cp = false;
elseif strcmp(name,'old_z')
sz = [obj.N 1];
dt = 'double';
cp = false;
else
error(['Error: Incorrect State Name: ', name.']);
end
end
% Code that executes each step
function [de_dz, de_dt_error] = stepImpl(obj, z, de_dt)
% Find dz/dt
dz = zeros(obj.N, 1) + z - obj.old_z;
obj.old_z = z;
% Find output of filter
de_dz = obj.A * z;
% Find filter error
de_dt_error = dot(de_dz, dz)/obj.dt - de_dt;
% Adapt the filter
end
function resetImpl(obj)
obj.A = zeros(obj.N);
obj.old_z = zeros(obj.N,1);
end
end
% Block icon and dialog customizations
methods (Static, Access = protected)
function header = getHeaderImpl
header = matlab.system.display.Header(...
'lmsSysObj', ...
'Title', 'Implicit Adaptor');
end
function groups = getPropertyGroupsImpl
upperGroup = matlab.system.display.SectionGroup(...
'Title','General',...
'PropertyList',{'Mu'});
lowerGroup = matlab.system.display.SectionGroup(...
'Title','Coefficients', ...
'PropertyList',{'Length of z','N'});
bottomGroup = matlab.system.display.SectionGroup(...
'Title','Time step', ...
'PropertyList',{'Controller time step (s)','dt'});
groups = [upperGroup,lowerGroup, bottomGroup];
end
end
methods (Access = protected)
function icon = getIconImpl(~)
icon = sprintf('Implicit Adaptor\n for bio-inpired controller');
end
function [in1name, in2name] = getInputNamesImpl(~)
in1name = 'Z Vector';
in2name = 'de/dt';
end
function [out1name, out2name] = getOutputNamesImpl(~)
out1name = 'de/dz';
out2name = 'de/dt error';
end
end
end
0 Comments
Accepted Answer
Brandon Stevens
on 14 Nov 2022
You are seeing this error when simulating via Code Generation because of how you defined the output variables. In the "getOutputSizeImpl" method you are using a variable not explicitly defined as 'Nontunable' with 'N'. The attribute of your system properties should be "Nontunable" otherwise when you run the Simulation in Code Generation mode, MATLAB Compiler assumes that those variables are tunable and thus assumes it is variable size.
For example, I would change your definition of Mu, N, and dt to be in "properties (Nontunable)" instead.
If you need any additional follow-up questions or need technical help, please contact Technical Support
0 Comments
More Answers (0)
See Also
Categories
Find more on Language Support 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!