MATLAB Answers

0

Preallocating of Structure without overwriting

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!

  0 件のコメント

サインイン to comment.

1 件の回答

Luna
Answer by Luna
on 28 Aug 2019
Edited by Luna
on 28 Aug 2019
 Accepted Answer

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 件のコメント

サインイン to comment.



Translated by