Error : index (1) is out of range (0..0)

Hello,
I want to save my results and display them into a new table of results but I get the error : index (1) is out of range (0…0).

Here is my code :

run(“Duplicate…”, “duplicate”);
run(“Median…”, “radius=2 stack”);
//run(“Brightness/Contrast…”);
run(“Enhance Contrast”, “saturated=0.35”);
run(“Apply LUT”, “stack”);
setAutoThreshold(“Default dark”);
//run(“Threshold…”);
//setThreshold(103, 255);
setOption(“BlackBackground”, false);
run(“Convert to Mask”, “method=Default background=Dark”);
run(“Erode”, “stack”);
run(“Erode”, “stack”);
run(“Dilate”, “stack”);
run(“Dilate”, “stack”);

run(“3D OC Options”, “volume surface nb_of_obj._voxels nb_of_surf.voxels integrated_density mean_gray_value std_dev_gray_value median_gray_value minimum_gray_value maximum_gray_value centroid mean_distance_to_surface std_dev_distance_to_surface median_distance_to_surface centre_of_mass bounding_box dots_size=5 font_size=10 show_numbers white_numbers store_results_within_a_table_named_after_the_image(macro_friendly) redirect_to=none”);
run(“3D Objects Counter”, “threshold=128 slice=39 min.=0 max.=255 exclude_objects_on_edges objects surfaces centroids centres_of_masses statistics summary”);

selectWindow(“Statistics for stack-0-1.hdr”)
X=Table.getColumn(“XM”)
Y=Table.getColumn(“YM”)
Z=Table.getColumn(“ZM”)

Table.rename(“Statistics for stack-0-1.hdr”,“Results”);
saveAs(“Results”, “/path/Results.csv”);
filestring=File.openAsString("/path/Results.csv");
line=split(filestring, “\n”);

X=newArray(line.length);
Y=newArray(line.length);
Z=newArray(line.length);

for (i=1; i<line.length; i++)
    {
        columns=split(line[i],"\n");
        X[i]=parseFloat(columns[1]); This is where the error occurs apparently
        Y[i]=parseFloat(columns[2]);
        Z[i]=parseFloat(columns[3]);
        print(X[i]+""+Y[i]+""+Z[i]);
    }

Thank you very much for you time, greatly appreciated.
Regards, Pierre

Hello Pierre -

You split filestring on the separator character "\n. So the
elements of the array line are strings that do not contain the
character "\n".

You then split line[i], also on "\n, a character that does not
appear in line[i], so columns becomes an array of length 1
containing just one string (namely line[i] itself).

IJM arrays are zero-base (indices run from 0 to length - 1), so
columns[1] is indeed out of bounds. (For columns – an array
of length 1 – only columns[0] would be valid.)

Did you perhaps mean to split line[i] on the character ","
(a comma)? filestring comes from “Results.csv”, so I image
that this is a text table whose rows are lines of text (separated
by "\n") and whose individual fields (“columns”) within a line
are separated by ",".

Thanks, mm

1 Like

Indeed, I changed these two lines :
line=split(filestring, “,”);

columns=split(line[i],",");

But still get the error, am I missing something obvious?

Regards, Pierre

Hello Pierre -

You are still doing two splits using the same separator (now
"," (comma)). So columns will still be an array of length 1.

I assume that your input file (read in as filestring) indeed
consists of lines separated by newlines, and columns separated
by commas. So you want two different separator characters in
your two splits: First split on "\n" (newline), and then split on
"," (comma).)

Also, remember that IJM arrays are zero-based, so if you do set

X[i]=parseFloat(columns[1]);

X[i] will be set to the second field in line[i], not the first
(where I use “first” and 'second" with their conventional meaning).
(This may be correct – it just depends on what you actually want.)

To help see what you are doing, you might print out the length
of the various arrays you create, e.g.:

print (lengthOf (line));
print (lengthOf (columns));

Do these length values match what you would expect based on the
contents of the file “Results.csv”?

Thanks, mm

The input file is the Results table, so it consists of numbers, I want in output the results of X, Y, Z with their values (XM, YM, ZM), so three columns with the number of rows corresponding to the number of objects detected.

I have detected 77 objects in this exemple, and get : lengthOf(line)=78 and lengthOf(columns)=25.

Thank you very much, the problem is solved and I understand it but I still struggle to obtain the desired result.