Index in position 2 exceeds array bounds error
1 次查看(过去 30 天)
显示 更早的评论
function my_function(x,it,dt)
global time;
global t;
if t == 1
output.Num_useful = 0;
output.Deno = 0;
end
....
% Start of time-simulation iterations
for k = 1:1:it
k;
if (time(t,1)/3600) < 24 % Check for the final time
t = t+1; % update global counter
time(t,1)= time(t-1,1) + dt; % update global time [s]
n1= time(t,1)/3600; % update time [h]
.
.
.
.
end
end
I am getting the following error
Index in position 2 exceeds array bounds.
Error in my_function (line 99)
if (time(t,1)/3600) < 24
How can I fix this?
0 个评论
回答(1 个)
Guillaume
2020-5-1
How can I fix this?
By ensuring that time has at least 1 column before calling your function. At the moment, it is empty since it doesn't even have one column.
Unfortunately, time and the poorly named t, are global and you've just fallen foul of one of the many problems with global variables. It's hard to keep track of their state since any piece of code can modify (or not modify when it should) the state of a global variable.
My recommendation would be to rewrite your code so it doesn't use global variables at all. Unfortunately, we don't have enough information about it give more advice.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!