How to Save Different Imported Excel Sheet Data to Same Variable
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I have several Excel sheets that I plan to import into Matlab one at a time to analyze individually (i.e. I'll import an Excel table, run my script. Then delete everything in the Workspace, import another Excel table, re-run my script. Etc.).
To preserve the original table, I save the table to a variable called 'T1'.
Each imported Excel sheet will have a different name, something like "Apple_Table_01", "Banana_Table_02", etc.
Currently, I have something like this:
T1 = Apple_Table_01
and then I'll erase 'Apple_Table_01' and type in 'Banana_Table_02' for the next time I need to run the case.
What I want is someting like this:
T1 = (Imported Table)
where (Imported Table) will automatically recognize to use the Excel table, no matter the name.
Is this possible?
I can explain further if this doesn't make sense.
Thanks.
0 个评论
回答(1 个)
Cris LaPierre
2023-8-8
I would create a list of my sheets, and then use a loop to load them. I would assign the output of the readtable command to T1.
sheets = ["Apple_Table_01","Banana_Table_02"];
for s = 1:length(sheets)
T1 = readtable("fileName.xlsx","Sheet", sheets(s));
...
end
3 个评论
Image Analyst
2023-8-8
help sheetnames
Cris LaPierre
2023-8-9
Also note that you can use numbers instead of names as the sheet name-value pair.
- Worksheet to read, specified as the comma-separated pair consisting of 'Sheet' and a positive integer indicating the worksheet index or a character vector or string containing the worksheet name. The worksheet name cannot contain a colon (:). To determine the names of sheets in a spreadsheet file, use sheets = sheetnames(filename). For more information, see sheetnames.
- Example: 'Sheet', 2
- Example: 'Sheet', 'MySheetName'
You can find a description of this and other input arguments for spreadsheets here: https://www.mathworks.com/help/matlab/ref/readtable.html#mw_0a3219fd-d40a-46a9-b230-7e249002f7b1
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!