MATLAB Answers

Error "Array indices must be positive integers or logical values" for functioning code

4 views (last 30 days)
Kouichi C. Nakamura
Kouichi C. Nakamura on 12 Oct 2018
Edited: Kouichi C. Nakamura on 12 Oct 2018
I'm using ImageJ via MATLAB to modify a series of large TIFF files (3.8 GB) in a for loop.
The loop works without error, so my code appears to be fine. However, after 3 or 5 iterations MATLAB crashes or issues an error. When an error is issued, it is often about a few particular lines. Below is one example of code snippet within the large for loop. Near the bottom, the line impArr(c).close; often, but not always, issues an error "Array indices must be positive integers or logical values".
impArr is a Java object array with four elements. nC is meant to be 4. So, the iterative variable c only takes 1, 2, 3, or 4. It's very unlikely to issue such an error (and indeed the same code works more often than not).
Bizarrely, when the error happened and MATLAB stopped in debug mode, I can execute the line impArr(c).close; without an error. This doesn't make any sense.
In addition, after the error or crash, when I retry the same code with the same iterative variable for the encompassing for loop, it always works without an error. Apparently, running the code for some time is necessary to cause this problem, and I suspect it is related to Java.
Although I was very concerned of Java Memory Heap size, now I'm using Collect Garbage command at the end of each iteration and the memory usage is close to 0% at the end of each for loop, so I'm not sure.
Is it something you often see that MATLAB makes very strange mistakes when using Java code with a large memory consumption???
impArr = javaArray('ij.ImagePlus',nC);
for c = 1:nC
impArr(c) = ij.IJ.createImage(newname,nX,nY,1,bitdepth);
XY = I0(:,:,c);
xy = XY(:)';
clear XY
switch bitdepth
case 16
ip = ShortProcessor(nX,nY);
ip.setPixels(xy);
impArr(c).setProcessor(ip);
case 8
ip = ByteProcessor(nX,nY);
ip.setPixels(xy);
impArr(c).setProcessor(ip);
otherwise
ip = FloatProcessor(nX,nY);
ip.setPixels(single(xy));
impArr(c).setProcessor(ip);
end
endz
rgbsm = ij.plugin.RGBStackMerge;
imp = rgbsm.mergeChannels(impArr,false);
for c = 1:nC
impArr(c).close; %TODO error happened
impArr(c).flush;
end
end
Here's another example. An error occurred in the line below: No method 'createImage' with matching signature found for class 'ij.IJ'.
impArr(c) = ij.IJ.createImage(newname,nX,nY,1,bitdepth);
However, in the debug mode, when I execute the same line, it didn't throw an error. The performance is somehow unreliable.

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by