Error when exectued from GUI, no error when executed in Command Window

1 view (last 30 days)
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.

Answers (1)

Walter Roberson
Walter Roberson on 23 Nov 2011
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 Comments
Jared
Jared on 24 Nov 2011
It's in 2 separate functions, and center span pts and resolution are all global (just between those 2 functions).
Walter Roberson
Walter Roberson on 24 Nov 2011
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!