Data size/type - Simulink

3 ビュー (過去 30 日間)
Rodolfo
Rodolfo 2012 年 3 月 27 日
Hello all,
I'm working on a project using Simulink. What I'm trying to do is to get the index of a vector using "find", e.g. k=find(u==max(u)), where "u" is the vector. However when I try to plug in "k" into other command line, e.g. y=u(k-1,k+1), the debugger pops up a message saying "k" is not scalar and it has dimension [?: x 1]. I haven't seen that before since using "numel" in Simulink doesn't issue that type of errors. Any hint is welcome. Thanks in advance.

回答 (1 件)

Kaustubha Govind
Kaustubha Govind 2012 年 3 月 27 日
I am assuming that you are using the MATLAB Function block to run this code in Simulink? It seems like the issue is that the output of the FIND function is a vector that can be of any length, and the length depends on how many elements in 'u' are equal to max(u). Typically, variables whose size could change at run-time need to be declared as variable-size using the coder.varsize directive, with the [size(u) 1] specified as the upper-bound for the size of 'k'. Even after you have done that, you need to account for the fact that 'k' could be a vector. In which case, something like y=u(k-1,k+1) will error out even in MATLAB.
  2 件のコメント
K E
K E 2012 年 3 月 27 日
If Rodolfo expects there to usually be only 1 maximum value of u, the Matlab function code could include the following line to enforce that k will always be a scalar:
if length(k) > 1
disp('Warning, more than 1 max value in u') ;
k = k(1) ;
end
Rodolfo
Rodolfo 2012 年 3 月 28 日
Thank you for your replies.
@K E: I've included your code snippet and it works nice. Thank you.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by