How can I add multiple excel files into a structure?

2 views (last 30 days)
I am trying to save multiple xlsx files into one structure. Each xlsx file represents a jump (biomechanics data from VICON) and I need to organize my structure in a way that each jump is saved in the same structure ("Data{i}=[];"). The goal for this code is to analyze each jump and compare the same variables (i.e. Lkneemoments in the x direction, Lkneeforce in the z direction) between the different jumps for the same subject. Obviously I will have multiple subjects so I need to have some sort of a field where I am able to locate the different jumps of each subject.
The following code works for one excel file, however I need to be able to load the rest of my files located in the same folder as 'drop_jump.xlsx'.
[num,txt,raw] = xlsread('drop_jump.xlsx');
%%
[R,C] = find(strcmpi('Frame',raw)==1);
for i=1:length(R)
Data{i}=[];
IndStart=R(i)+find(num(R(i):end,1)==1,1,'first')-1;
IndEnd=R(i)+find(isnan(num(IndStart:end,1)),1,'first')-1;
if isempty(IndEnd)
IndEnd=size(num,1);
end
Data{i}.Frame=num(IndStart:IndEnd,1);
Data{i}.SubFrame=num(IndStart:IndEnd,1);
Label=raw(IndStart-2,3);
SubLabel=raw(IndStart-1,3);
Units=raw(IndStart,3);
for j=3:size(raw,2)
if ~isempty(raw{IndStart,j})&~isnan(raw{IndStart,j})
Units=raw(IndStart,3);
if ~isempty(raw{IndStart-1,j})
SubLabel=raw(IndStart-1,j);
SubLabel = erase(SubLabel,[" ","-",":","|","(",")"]);
end
if ~isempty(raw{IndStart-2,j})&~isnan(raw{IndStart-2,j})
Label=raw(IndStart-2,j);
Label = erase(Label,[" ","-",":","|","(,",")"]);
Data{i}.(Label{1})=[];
end
Data{i}.(Label{1}).(SubLabel{1})=num(IndStart:IndEnd,j);
end
end
end
Any sort of help or feedback is much appreciated.
  3 Comments
dpb
dpb on 10 Aug 2022
Wowsers!!! That IS a complex workbook, indeed! It'll take a whole lot of parsing to make anything useful out of that structure, indeed -- although I still don't think burying it inside another structure is likely to be of a lot of help even if you get it stuffed into one.
I guess some sort of generic idea of what needs to be done with the data might help to design a data structure although indeed, when one has multiple cases of same variable the need for the variable names to be unique is somewhat of a disadvantage with table. But, they've still got to be unique in any other format as well -- or just not named and referenced by indices. There might be where a struct or a table inside a table(*) might be and advantage.
(*) Indeed, one can, in fact, have a column variable in a table be another table -- there might be the way to handle the various types. Or, there could be an array containing the types.
I've got another commitment just now; I'll try to look at the spreadsheet in some more detail later on...

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 10 Aug 2022
Though I would also recommend a solution that uses readtable over xlsread, if you have code that works already, why not turn it into a function and use a datastore to load the data? See our Importing Multiple Data Files video from our Practical Data Science specialization on Coursera for more.
ds = fileDatastore("drop_jump*.xlsx","ReadFcn",@importJump,"UniformRead",true);
Data = readall(ds);
  4 Comments
Leutrim Mehmeti
Leutrim Mehmeti on 16 Aug 2022
@Cris LaPierre that worked better than I thought it was going to work. Thank you very much.
@dpb Okay let me play with the my code and see if I am able to come up with something similar to what you are suggesting. Thank you again for your help and feedback.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!