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.")
);
我收到相同的錯誤訊息:
範圍資料表存取需要在存取或評估 'dt1:Column' 、 dt1:Column( colIndex ) /*###*/ 時第 1 行有資料表列或變量
在第 49 行
引用列的語法不正確(您正在組合 Column() 和 As Column() (表:列語法))。
改變
dt2:Column(j)[i - startRow + 1] = dt1:Column(colIndex)[i];
到
Column(dt2, j)[i - startRow + 1] = Column(dt1, colIndex)[i];
可能會起作用。
此外,可能有更簡單的方法來實現您想要實現的目標,但這需要我們查看範例文件和想要的結果。
正如 Jarmo 所指出的,對於某些步驟,JMP 提供了預先定義的模組。
透過使用此類模組:
- 程式碼變得更短並且更容易理解
- JMP 自動處理資料類型
- 執行速度要快得多
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 )
);