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.

回答(1 个)

Walter Roberson
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 个评论
Jared
Jared 2011-11-24
It's in 2 separate functions, and center span pts and resolution are all global (just between those 2 functions).
Walter Roberson
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by