Snippets

Dave Mason Rearrange CSV file results IJ Forum [https://forum.image.sc/t/copying-entire-columns-into-new-files-fiji-macro/45182?u=dnmason]

Created by Dave Mason

File sortData.ijm Added

  • Ignore whitespace
  • Hide word diff
+MeasurePath = getDirectory("Input Folder");
+filelistMeasure = getFileList(MeasurePath);
+
+fileX = File.open(MeasurePath + "output.txt"); // display file open dialog
+
+
+outColumn=0;
+
+for (i=0; i< filelistMeasure.length; i++) {
+	// select specific files using naming condition
+	if (startsWith(filelistMeasure[i], "M_")) {
+	//-- record the column number for output
+	outColumn++;
+		//-- Read in the file as a string
+		fileData=File.openAsString(MeasurePath + filelistMeasure[i]);
+				
+		//-- Split the whole thing into lines
+		fileData_lines=split(fileData, "\n");
+				
+		//-- count how many lines we have and create a new array to hold one columns worth of values
+		numLines=fileData_lines.length;
+		ArrayCXum=newArray(numLines);
+		//ArrayCYum=newArray(numLines);
+		//ArrayCZum=newArray(numLines);
+		
+		//-- loop through each line
+		//-- ### IF YOU DON'T WANT TITLES IN THE OUTPUT START k AT 1 ###
+		for (k = 0; k < numLines; k++) {
+		//-- For each line, split it using the comma delimiter
+			fileData_columns=split(fileData_lines[k],",");
+		//-- Store the third column into the outputArray
+			ArrayCXum[k]=fileData_columns[8];
+			//ArrayCYum[k]=fileData_columns[9];
+			//ArrayCZum[k]=fileData_columns[10];
+
+			// this would be alright, but it writes arrays underneath
+			//print(fileX, ArrayCXum[k] + " \t");
+		}
+		//-- print the array out to the results table
+		for (l=0;l<ArrayCXum.length;l++){
+			//-- iterate across the variable length array and store in the results table
+			setResult(filelistMeasure[i], l, ArrayCXum[l]);
+			updateResults();
+		}
+		
+	}
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.