Select Random columns from a matrix as a signal in Simulink

2 ビュー (過去 30 日間)
Nicholas Lindenfeldar
Nicholas Lindenfeldar 2022 年 1 月 18 日
I have a matrix 360x9 in a from workspace block. By specifying a constant block with a value n, I would like to output n columns randomly selected from the data as an output and plot them on a scope. I have tried utilizing a MATLAB function block and using the following code
function y = fcn(u, data)
ncol=u
s=randi(size(data,2),1,ncol);
y=data(:,s)
end
This logic works in matlab alone, however when implemented into a MATLAB function block I recieve an error stating that y is inferred as a variable-size matrix and must be defined in terms of non-tunable parameters or specify variable size. If I specify variable size in the model explorer, the output signal has a nondiscrete sample time, and it must be discrete. Any help would be greatly appreciated. Thanks.

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2022 年 1 月 18 日
  1. The number of columns "u" comes from an input of the MATLAB Function block, so the data involved IS variable size.
  2. The MATLAB Function block is executed once at every simulation step. Are you sure this is what you want?
  3. A better approach is to process this randomness as initialization and then input the processed data into Simulink.
  1 件のコメント
Nicholas Lindenfeldar
Nicholas Lindenfeldar 2022 年 1 月 24 日
Thanks, I didn't think of that. I was able to implement it as an initialization script.

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by