How to pick a random number from a set of numbers in simulink?

2 ビュー (過去 30 日間)
Sudipta
Sudipta 2023 年 11 月 19 日
編集済み: Fangjun Jiang 2023 年 11 月 20 日
I want to pick a number from a set of number, i.e [1011, 1001, 1111, 1101, 0011] once at the beginning of each simulation. But i am not getting any help from the random number generator blocks. MATLAB code works in this case, so I've used matlab fuction block to run the code in simulation. But the code keeps running and generating value. Is there any fix to this?
The code I'm using in the fuction block is:
function data = y
numbers = [1011, 1001, 1111, 1101, 0011, 0101, 1100];
% Generate a random index
index = randi(length(numbers));
% Display the random number
data=(numbers(index));

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2023 年 11 月 20 日
編集済み: Fangjun Jiang 2023 年 11 月 20 日
The MATLAB Function block is like any other blocks. It will execute at every simulaiton step.
The best way is to put this MATLAB Function block inside the Initialize Subsystem.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by