Create a vector in Simulink

145 ビュー (過去 30 日間)
Kai Yang
Kai Yang 2019 年 6 月 5 日
回答済み: Hamid Satarboroujeni 2023 年 9 月 29 日
I am trying to create a vector using constant block in simulink. My problem is that the size of the vector Nvec is calculated by the other blocks during the simulation. In other words, the size of vector Nvec is unknown before simulation starts. Therefore, I got the error "Undefined function or variable 'Nvec'" as I cannot use a unknown variable in defining the constant blcok. Any idea how to solve this issue, or, is there other ways to achieve my purpose.
Thanks!

回答 (2 件)

Noam Greenboim
Noam Greenboim 2019 年 12 月 31 日
Simulink does not allow you the same freedom as Matlab, in terms of changing the size and type of parameters.
There are a few ways to approach it.
  1. Create a vector of the maximum size, and populate the remaining elemens with NaN (optionally). Maintain a list of the valid indices, in order to address the relevant elements only.
  2. Work with embedded Matlab function. You would still need to define the size of the vector, but maybe working directly with code would be easier to you.
  3. Track the execution order of the blocks, and add a unit delay, so you force the calculation of Nvec first.

Hamid Satarboroujeni
Hamid Satarboroujeni 2023 年 9 月 29 日
If you don't know the upper bound of Nvec and don't want to deal with NaNs and possibly variable-sized signals, you can use other Simulink blocks to create the vector. Let's say you have a signal X of size Nvec-by-M and M is a known constant, then you can use the Matrix Multiply block to multiply X by a constant, say zeros(M,1) in a Constant block. The output of the Matrix Multiply block will be a Nvec-by-1 vector. Now, you can use the Assignment block to write values to the Nvec-by-1 vector.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by