I have the following cell array which is a list of some (but not all) of the tab names in an excel file:
chosenTabs =
'Screen' 'SectorAbsolute' 'SectorRelative'
How do I get it to read off each tab of the excel sheet according to what is on this list and return a table of the tab's contents? The new tables should have the same name as the tab that is read.
I have tried (for example to create a table called 'SectorAbsolute' containing the contents of the tab 'SectorAbsolute'):
char(chosenTabs(2))=readtable(inputFile,'Sheet',char(chosenTabs(2)))
but this returns the error:
You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.
A method utilizing a structure array:
chosentabs = {'Sheet1', 'Sheet3'};
ntabs = length(chosentabs);
for ii = 1:ntabs
mytables.(chosentabs{ii}) = readtable('test.xlsx', 'Sheet', chosentabs{ii});
end
Which returns mytables
, a structure array containing your tables. You can access your sheets explicitly (e.g. mytables.Sheet1
) or by utilizing dynamic field referencing (e.g. mytables.(somestring)
)