How to load excel sheets into a struct
조회 수: 14 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Raunak Gupta
2020년 2월 17일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!