How to send one array element at a time to a subsystem every 0.5 seconds in Simulink?

5 ビュー (過去 30 日間)
Hi all, I'm working on a Simulink model where a MATLAB Function block outputs an array of control commands (the command array is generated once at the start of simulation). I’d like to send one command at a time from this array to a subsystem input, with a rate of 2 Hz (i.e., one value every 0.5 seconds).
What’s the best way to implement this?
Thanks in advance!
  3 件のコメント
Ben Navon
Ben Navon 2025 年 4 月 16 日
@Mathieu NOE I'm getting this error from the Unbuffer block:
Error:Error in 'go2_pp/Unbuffer': All sample times must be discrete. No continuous or constant sample times are allowed.
Mathieu NOE
Mathieu NOE 2025 年 4 月 16 日
as it says , you can only use this block with fixed sampling rates

サインインしてコメントする。

採用された回答

Fangjun Jiang
Fangjun Jiang 2025 年 4 月 14 日
Nothing special. Use a Counter block (free-running or limited) in combination with the model solver setting to provide the step (every 0.5 second). Feed it to this function.
function y = fcn(u)
persistent cmd
N=10;
if isempty(cmd)
cmd=zeros(N,1);
end
if u==0
cmd=10*(1:N);
y=0;
elseif u>=N
y=cmd(N);
else
y=cmd(u);
end
end
  2 件のコメント
Ben Navon
Ben Navon 2025 年 4 月 16 日
Thanks for your response! I'm not sure I fully understand the architecture you're recommending.
What exactly should I feed into your function? Where should the function's output be connected?
For context, here is my current model:
  • The MATLAB Function block (which generates the commands vector) is inside an Enabled Subsystem.
  • I'm using a Counter block with a 0.5 [s] sample time (default 16-bit).
  • The Rate Transition block has its output port sample time set to 0.5 [s].
  • My simulation duration is 60 seconds.
Model solver settings:
Also, I'm getting this error from the Unbuffer block:
Error:Error in 'go2_pp/Unbuffer': All sample times must be discrete. No continuous or constant sample times are allowed.
Fangjun Jiang
Fangjun Jiang 2025 年 4 月 16 日
Feed the Counter block to the MATLAB Function block input.
The function output is the "one array element at a time".

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEvent Functions についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by