如何在Simulink中将值放入矩阵?例如,如果我每0.5s采样一个正弦波,然后将该值放入大小为N的固定大小的矩阵中。矩阵填满后,它将覆盖最早的值。
假设您正在谈论将矩阵当作缓冲区使用,那么如果您具有DSP模块集,则可以使用缓冲区模块。否则,使用MATLAB功能块可以很直接地做到这一点(请参见下面的代码)。如果矩阵是由模型的另一部分构成的,则可以将其作为第二个输入传递到MATLAB Function块,并适当修改(新元素)插入代码。
function y = custom_buffer(u)
%#codegen
persistent buffer next_index
buf_size = 1000;
% Define initial values
if isempty(buffer)
buffer = zeros(buf_size,1);
next_index = 1;
end
% Populate the buffer
buffer(next_index) = u;
% Increment the location to write to at the next time
if next_index < buf_size
next_index = next_index + 1;
else
next_index = 1;
end
% Populate the output
y = buffer;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句