MATLAB: Import/convert an Excel file with more worksheets into a structure with more fields, one field for each worksheet

MATLABstructure

Hi All,
I would like to import/convert an excel file which has n worksheets, into a structure with n fields, like S.f1, S.f2, …, S.fn, one field for each worksheet of the Excel file, in MATLAB.
Is there anyone who could help me?
Thanks,
Mike

Best Answer

Generally, importing multiple excel sheets is done by calling the xlsread command multiple times. One of the flags of the command is for designating which sheet you are using. The command does not import the information into a structure array, but you can reorganize the information from the imported matrix into a structure array easily enough.
for i = 1:nsheets
[~ ~ data] = xlsread('myexcelfile.xlsx',i);
[mystruct.data1] = data{range1};
[mystruct.data2] = data{range2};
[mystruct.data3] = data{range3};
[mystruct.data4] = data{range4};
end
Obviously you need to make your own ranges and structure fields, but the basic concept is sound.