MATLAB Answers

Preallocating of Structure without overwriting

4 views (last 30 days)
Joel Affolter
Joel Affolter on 28 Aug 2019
Edited: Luna on 28 Aug 2019
I have multiple Excel Files with the same structure but different values depending on the experiment. I load a bunch of those files into MatLab using
[numdata, textdata, ~] = xlsread(...)
Then I assign determine the number of files loaded and make a for loop to transfer the numerical data to the i-th index of my structure. This looks something like:
AutarkyData(i).ReactionTime = numdata(j)
Once I imported all my data and stored it appropiately, I calculate some values and also store them under AutarkyData (still in the for loop). Once all calculations are done, the result is exported to a new Excel File.
Everything is working so far, but the whole procedure gets increasingly slow once a multidude of files is imported. This is most probably due to the fact that I need to add new fields to the structure AutarkyData in the for loop, as it needs to add a new "column" to the structure with each new numdata it needs to import.
How do I preallocate my structure with the amount of files I selected without overwriting the existing values (maybe I want to add some values later)? I have an idea with how this would work with vectors/tables, but the syntax/buildup of a structure confuses me.
Thanks a lot in advance!

  0 Comments

Sign in to comment.

Accepted Answer

Luna
Luna on 28 Aug 2019
Edited: Luna on 28 Aug 2019
I can recommend you to use datastore to process multiple files at once. This will be the faster than using xlsread. Actually it might be the fastest one.
You can read those links below:
A sample example:
ssds = spreadsheetDatastore('C:\Users\...\data');
data = ssds.readall;
It returns you a table containing to your data. Also you can specify includesubfolders or file extentions, etc.
Later on if you want a struct you can use table2struct function to convert it.

  0 Comments

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!

Translated by