Clear Symbols();
// Open a file dialog to choose the text file
txtFilePath = Pick File("Choose a text file", {"Text Files|*.txt"});
// Check if a file was selected
If(txtFilePath != "",
// Convert the file path to ensure it's in the correct format
txtFilePath = Convert File Path(txtFilePath);
// Import the text file into a new JMP table
dt1 = Open(
txtFilePath,
Delimiter("\t") // Change delimiter if necessary
);
// Specify the row to use for column names (e.g., row 1)
columnNamesRow = 16;
// Specify the columns to extract (e.g., columns 1, 2, 3)
columnsToExtract = {1, 17, 18, 22}; // Adjust column indices as needed
// Extract the column names from the specified row and columns
columnNamesList = {};
For(j = 1, j <= N Items(columnsToExtract), j++,
colIndex = columnsToExtract[j];
columnNamesList[j] = :Column(colIndex)[columnNamesRow];
);
// Create a new JMP table with the extracted column names
dt2 = New Table("Converted Table");
For(i = 1, i <= N Items(columnNamesList), i++,
dt2 << New Column(Char(columnNamesList[i]), Character); // Adjust column type as needed
);
// Specify the rows to extract (e.g., rows 2 to 10)
startRow = 17;
endRow = 65;
//
// Loop through the specified rows and add them to the JMP table
For(i = startRow, i <= endRow, i++,
dt2 << Add Rows(1);
For(j = 1, j <= N Items(columnsToExtract), j++,
colIndex = columnsToExtract[j];
dt2:Column(j)[i - startRow + 1] = dt1:Column(colIndex)[i];
);
);
// Close the imported table
Close(dt1);
,
// If no file was selected, show a message
Show Message("No file selected.")
);
Ricevo gli stessi messaggi di errore:
L'accesso alla tabella dati con ambito richiede una colonna o una variabile della tabella dati alla riga 1 nell'accesso o nella valutazione di 'dt1:Column' , dt1:Column( colIndex ) /*###*/
alla riga 49
La sintassi per il riferimento alle colonne non è corretta (si stanno combinando Column() e As Column() (sintassi table:column)).
Cambiando
dt2:Column(j)[i - startRow + 1] = dt1:Column(colIndex)[i];
A
Column(dt2, j)[i - startRow + 1] = Column(dt1, colIndex)[i];
potrebbe funzionare.
Potrebbero esserci anche metodi più semplici per ottenere ciò che desideri, ma per farlo è necessario che tu veda un file di esempio e il risultato desiderato.
Come indica Jarmo, per alcuni passaggi JMP fornisce moduli predefiniti.
Utilizzando tali moduli:
- il codice diventa più corto e più facile da capire
- JMP gestisce automaticamente i tipi di dati
- l'esecuzione è molto più veloce
txtFilePath = "$SAMPLE_IMPORT_DATA/Animals_line3.txt";
columnNamesRow = 3;
columnsToExtract = {"species", "miles"};
startRow = 2;
endRow = 10;
dt1 = Open( txtFilePath,
Import Settings( Labels( 0 ) )
);
wait(0); //let JMP relax before it continues with the next steps<BR />
if(columnNamesRow>1, dt1 << delete rows( 1 :: (columnNamesRow - 1) ));<BR />dt1 << move up;
dt2 = dt1 << Subset(
Rows( startRow :: endRow ),
columns( columnsToExtract )
);