Preallocating of Structure without overwriting

조회 수: 4 (최근 30일)
Joel Affolter
Joel Affolter 2019년 8월 28일
편집: Luna 2019년 8월 28일
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!

채택된 답변

Luna
Luna 2019년 8월 28일
편집: Luna 2019년 8월 28일
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개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by