"Index exceeds array dimensions. Index value 2 exceeds valid range [1-1] for array" --How to solve this?
31 ビュー (過去 30 日間)
古いコメントを表示
Sunandan Adhikary
2020 年 10 月 18 日
コメント済み: Sunandan Adhikary
2020 年 10 月 22 日
Whenever I am trying to update a 2-dimensional array in a loop matlab throws this error!
%% initialized in workspace
s.t(1)=0*0.1;
%% update via a function
for i=1:10 % implemented via simulink counter and increment block
func op= fcn(i)
s.t(i+1)=i*0.1
end
end
0 件のコメント
採用された回答
Walter Roberson
2020 年 10 月 21 日
In Simulink,
s.t(1)=0*0.1;
defines s.t as being a scalar value that is not permitted to increase in size.
In Simulink, you need to initialize variables to the maximum size they could use. You can reduce the size of the variable later if you need to, but you cannot increase it in size. (See, though, coder.varsize() )
Your code runs to s.t(10+1) so your s.t is length 11. Initialize it as
s.t = zeros(11,1);
その他の回答 (1 件)
Prudhvi Peddagoni
2020 年 10 月 21 日
Hi,
The error you mentioned occurs when you try to access an array element which is out of it's length.
If you are trying to update s.t array such that s.t(i+1)=i*0.1
s.t(1)=0*0.1;
for i=1:10
s.t(i+1)=i*0.1
end
if not, can you post more information about what you are trying to do
Hope this helps.
参考
カテゴリ
Help Center および File Exchange で Simulink Functions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!