Error when exectued from GUI, no error when executed in Command Window
1 次查看(过去 30 天)
显示 更早的评论
I'm having a hard time tracking this issue down.
I have four 1x15 doubles:
for i=1:15
center(i)=double(pdata{i}{6});
span(i)=double(pdata{i}{7});
pts(i)=double(pdata{i}{9});
resolution(i)=span(i)/(pts(i)-1);
end
pdata is a 1x15 cell, and each of those cells is a 1x18 cell. I have 15 parameter files (each with 18 parameters) and I am storing 3 of them, and calculating another. That all works fine.
I then calculate a few more things:
for i=1:15
minf(i)=(center(i)-span(i)/2);
for j=1:pts(i)
f{i}(j)=minf(i)+(j-1)*resolution(i);
end
<other stuff>
end
Eventually I create a plot. Everything works as it should when I copy it and run it through the command window. However, when I run it from my GUI, which runs the same exact code, I get the error (triggered by line 2 in second section of code):
"??? Attempted to access center(1); index out of bounds because numel(center)=0."
However, when I run it through the command window (when it executes as it should) and do numel(center), ans=15 as it should.
0 个评论
回答(1 个)
Walter Roberson
2011-11-23
Is all of the code you note in one continuous function? Or is it split between two functions, with the second function perhaps having a "global center" statement ?
2 个评论
Walter Roberson
2011-11-24
I suspect you will find that you did not declare it as global in the first function, but did declare it as global in the second function. When you declare a variable as global when it has not previously been defined as a global variable and given a value, then "global" will initialize the new global variable to the empty array.
Keep in mind that you must declare a variable as "global" in EVERY routine you use it in.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!