Expandable array

Folks,

Writing function to isolate tif files from text files and return array with just tiff files. From macro, list of all files (tif and text) are passed to “get_imagefile(filelist).” Within the function, I created an array variable (pathfile2) that will contain tif files. Being that the tif file array length may be initially unknown, I used the expandable array functions. Version 1.53c Fiji is being used so “setOption(“ExpandableArrays”, true)” is used.

My issue is that when the tiff array is returned (line 35 in code), it contains a NaN line. When the main macro tries to process that array, the program does not execute properly.

I can obviously bypass an expandable array command and just create an array with length equal to master list (both tif and text). What results in the final array are tif files with the rest of the array padded with "0"s. I can use the “Array.deleteValue()” before being returned, but I was looking for a more straight forward solution.

Try to use a counter inside your loop:

...
cnt = 0;
for (i=0; i<filelist.length; i++){
     if (endsWidth(filelist[i]; ".tif")){
          cnt++;
          pathfile2[cnt] = dir1 + filelist[i];
          ...
}

Peter,

Thanks for the suggestion. However, per the “Array.show” for “pathfile2” there is an extra index added (blank row). When this handed back to the main body, a “length” command confirms 3 elements. When this array is processed in line 22, a NaN is confirmed occupying first element, while the desired tif files follow.

Just for reference the original folder has 2 tiffs and one text. The “get_imagefile” looks to correctly grab the tif files, but the array should have 2 elements and not three.

Neil

@NRW Sorry, my mistake. It must be:

...
cnt = 0;
for (i=0; i<filelist.length; i++){
     if (endsWidth(filelist[i]; ".tif")){
          pathfile2[cnt] = dir1 + filelist[i];
          cnt++;
          ...
}

BTW: It would be helpful if you post your code and not just a screenshot. You can use the </> icon to insert preformatted text.

1 Like

Peter,

Definitely last suggestion populated first three elements with tif files.
However, I’m still getting an extra element within the array when there should only be three tif files (“Array.show” before "return).

C:/Users/nrwillia/Desktop/IAtmp/source/11793-122-D-area 1-5Kv-100x_raw.tif
C:/Users/nrwillia/Desktop/IAtmp/source/11793-122-D-area 2-5Kv-100x_raw.tif
C:/Users/nrwillia/Desktop/IAtmp/source/11793-122-D-area 3-5Kv-100x_raw.tif
0

function get_imagefile(filelist) {				//select image tif files 
	setOption("ExpandableArrays", true);
	pathfile2 = newArray;
	cnt = 0;
	pathfile2 = newArray(filelist.length);
	for (i = 0; i < filelist.length; i++) {
		
		if (endsWith(filelist[i], ".tif")) {	
			pathfile2[cnt] = dir1 + filelist[i];
			cnt++;	
		}		
	}
	//pathfile2 = Array.deleteValue(pathfile2, 0);
	Array.show(pathfile2);
	return pathfile2;						
 }

Pls post all relevant parts of your code.

The issue is you are initiating pathfile2 twice.

pathfile2 = newArray(filelist.length);

So the first time it’s got no length and the second time you are making it the length of your file list which explains why you have an empty entry at the end.

2 Likes

Laura,

Works perfectly!
Thanks for the heads up. I must have gotten blurry eyed to not catch that issue.

Neil

1 Like

Peter,

Thanks again for all your help.

Neil