Preallocating of Structure without overwriting

2 visualizaciones (últimos 30 días)
Joel Affolter
Joel Affolter el 28 de Ag. de 2019
Editada: Luna el 28 de Ag. de 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!

Respuesta aceptada

Luna
Luna el 28 de Ag. de 2019
Editada: Luna el 28 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by