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

조회 수: 1 (최근 30일)
Jared
Jared 2011년 11월 23일
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