How to load excel sheets into a struct

19 views (last 30 days)
Cameron Kirk
Cameron Kirk on 12 Feb 2020
Answered: Raunak Gupta on 17 Feb 2020
Hi,
I've created a code (see below), which successfuly so far enables me to access each subjects excel file, then each sheet within that excel file. Each subject has a varying number of sheets within their Excel file, with the first sheet for each subject always named 'Summary', then the sheets are named Trial1, Trial2, Trial3 etc.... each sheet represents a seperate trial.
I wish to use the data from each individual sheet, then put them into a struct which is named gait analysis report, then which subject number they are and finally which sheet it is.
So it may look like: GaitAnalysis Report - Subject1 - Trial 1 - Data for trial
The SheetData is in a table format and when I try to convert this to a struct I get the message 'Scalar structure required for this assignment'
Can someone please help?
for iExcelSubject = 1:size(ExcelFilePatients,1);
GaitReport = ExcelFilePatients(iExcelSubject).name; %loads gait file
[~,SheetNames] = xlsfinfo(GaitReport); %Takes all sheet names from the excel document
nSheets=numel(SheetNames);
%Loop over all sheets
for iSheetData = 1:nSheets
SheetData = readtable(GaitReport,'sheet',iSheetData); %Loads each sheet
GaitAnalysisReport.(iExcelSubject).(iSheetData) = table2struct(SheetData);
end
end

Answers (1)

Raunak Gupta
Raunak Gupta on 17 Feb 2020
Hi,
The Field names of structure you are trying to create need to be a variable name instead of integers and double value that are currently in the code while executing the for loop. It is suggested to use variable name as a field for structure otherwise dot indexing error will pop up.
Also, as you are trying to create a structure to contain all the data for lookup purposes the error mentioned should not occur. It appears only when it is attempted to modify the existing value of a structure field. If that is required following documentation may help.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!