Clear Filters
Clear Filters

Add structs in a saved array of structures

2 views (last 30 days)
I have a function (fun1) which generates several results (r1,r2,r3...). I organise them in a struct.
This struct is the output of the funtion.
I have to run this same function for several variables and I want to keep all the results in the same array of structs. Therefore could I do:
However, the function deals with huge sets of data so it can take on the order of 45 minutes to run, and I have to run it more than 20 times. Is there any way to save the output struct so that each time that I run the function it adds the output to the previous ones? I mean to do something similar to what I do in the workspace but directly in a .mat file.

Accepted Answer

Mohammad Sami
Mohammad Sami on 28 Jan 2020
You can use the matfile function to write directly to a mat file
a = matfile('newfile.mat','Writable',true);
n = 10;
for i = 1:n
% your code
a.mystruct(i,1) = struct('abc',123,'def',456);
clear a;
Stephen23 on 28 Jan 2020
Edited: Stephen23 on 28 Jan 2020
Unless the structure itself** is huge, iterating in reverse is unlikely to make much difference. But you can experiment and tell us the results.
** not the data it contains!
Miquel on 28 Jan 2020
Ok, thanks. No, I generate 2 stucts that are only 7 fields and around 20... rows? So I guess it should make almost no difference. And I will be using the computer most of the time while Matlab is running. Thanks both!

Sign in to comment.

More Answers (0)


Find more on Structures in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!