simulink 中的大小分配
size allocation in simulink
一般在simulink中,写'Matlab fcn'的时候需要预先分配大小。在函数中,可以使用
声明
coder.varsize('var',[n, m]);
其中 n 和 m 给出了最大可能的大小。我遇到的问题是以下功能:
function y = fcn(u)
coder.varsize('tmp',[1, 100])
test = ((abs(diff(u)))*100 > 10);
i1 = find(test,1,'first');
i2 = find(test,1,'last');
while i2>=i1
i1 = max(1,i1-1);
i2 = i2+1;
tmp = u(i1:i2);
t1 = conv(tmp,[1 2 1]/4);
t2 = t1(2:end-1);
t2(1) = t2(1)+t1(1);
t2(end) = t2(end)+t1(end);
u(i1:i2) = t2;
test = ((abs(diff(u)))*100 > 10);
i1 = find(test,1,'first');
i2 = find(test,1,'last');
end
y = u;
end
因为 tmp 的大小取决于 i1 和 i2 的值.一般用coder.varsize声明应该没有问题,但是这次不知道怎么实现了。
如果您需要更多信息,请告诉我!
编辑:如果我修复函数的输出大小,我得到
Errors occurred during parsing of MATLAB function 'MATLAB Function'
如果我将函数的输出大小设置为 'inherited',我会得到
Error in default port dimensions function of S-function 'MATLAB Function'. This function does not fully set the dimensions of output port 3
我怀疑有几件事正在发生。
首先,代码生成器不知道您在 find
中使用的可选输入会导致标量输出。 (实际上它不是因为输出可能为空或标量。)要克服此问题,请使用 find
而不使用任何可选输入,然后在定义 i1
和 [ 时提取第一个和最后一个元素=14=].
其次,您需要正确处理 find
执行 return 空输出的情况。我已在下面的示例代码中完成此操作,但您几乎肯定需要修改它以适应您在实际模型中需要的特定逻辑。
function y = fcn(u)
coder.varsize('tmp',[1, 100]);
coder.varsize('find_idx',[1, 100]);
test = ((abs(diff(u)))*100 > 10);
find_idx = find(test);
if ~isempty(find_idx)
i1 = find_idx(1); % find(test,1,'first');
i2 = find_idx(end); %find(test,1,'last');
while i2>=i1
i1 = max(1,i1-1);
i2 = i2+1;
tmp = u(i1:i2);
t1 = conv(tmp,[1 2 1]/4);
t2 = t1(2:end-1);
t2(1) = t2(1)+t1(1);
t2(end) = t2(end)+t1(end);
u(i1:i2) = t2;
test = ((abs(diff(u)))*100 > 10);
find_idx = find(test);
if ~isempty(find_idx)
i1 = find_idx(1); % find(test,1,'first');
i2 = find_idx(end); %find(test,1,'last');
else
break
end
end
y = u;
else
y = u;
end
一般在simulink中,写'Matlab fcn'的时候需要预先分配大小。在函数中,可以使用
声明coder.varsize('var',[n, m]);
其中 n 和 m 给出了最大可能的大小。我遇到的问题是以下功能:
function y = fcn(u)
coder.varsize('tmp',[1, 100])
test = ((abs(diff(u)))*100 > 10);
i1 = find(test,1,'first');
i2 = find(test,1,'last');
while i2>=i1
i1 = max(1,i1-1);
i2 = i2+1;
tmp = u(i1:i2);
t1 = conv(tmp,[1 2 1]/4);
t2 = t1(2:end-1);
t2(1) = t2(1)+t1(1);
t2(end) = t2(end)+t1(end);
u(i1:i2) = t2;
test = ((abs(diff(u)))*100 > 10);
i1 = find(test,1,'first');
i2 = find(test,1,'last');
end
y = u;
end
因为 tmp 的大小取决于 i1 和 i2 的值.一般用coder.varsize声明应该没有问题,但是这次不知道怎么实现了。
如果您需要更多信息,请告诉我!
编辑:如果我修复函数的输出大小,我得到
Errors occurred during parsing of MATLAB function 'MATLAB Function'
如果我将函数的输出大小设置为 'inherited',我会得到
Error in default port dimensions function of S-function 'MATLAB Function'. This function does not fully set the dimensions of output port 3
我怀疑有几件事正在发生。
首先,代码生成器不知道您在 find
中使用的可选输入会导致标量输出。 (实际上它不是因为输出可能为空或标量。)要克服此问题,请使用 find
而不使用任何可选输入,然后在定义 i1
和 [ 时提取第一个和最后一个元素=14=].
其次,您需要正确处理 find
执行 return 空输出的情况。我已在下面的示例代码中完成此操作,但您几乎肯定需要修改它以适应您在实际模型中需要的特定逻辑。
function y = fcn(u)
coder.varsize('tmp',[1, 100]);
coder.varsize('find_idx',[1, 100]);
test = ((abs(diff(u)))*100 > 10);
find_idx = find(test);
if ~isempty(find_idx)
i1 = find_idx(1); % find(test,1,'first');
i2 = find_idx(end); %find(test,1,'last');
while i2>=i1
i1 = max(1,i1-1);
i2 = i2+1;
tmp = u(i1:i2);
t1 = conv(tmp,[1 2 1]/4);
t2 = t1(2:end-1);
t2(1) = t2(1)+t1(1);
t2(end) = t2(end)+t1(end);
u(i1:i2) = t2;
test = ((abs(diff(u)))*100 > 10);
find_idx = find(test);
if ~isempty(find_idx)
i1 = find_idx(1); % find(test,1,'first');
i2 = find_idx(end); %find(test,1,'last');
else
break
end
end
y = u;
else
y = u;
end