How to save an array output to file every 1000 iterations?

12 views (last 30 days)
My array is 201 by 201. And I want to watch it evolve by looking at every 1000 steps in time. Basically I have an initial array and then every time step it evolves based on an operator that I defined for it. I don't have enough memory in matlab to save every iteration of the array, so I've only got the last 5 saved at a time. What I want to do is when the time step reaches 1000,2000,3000, etc to save in a seperate file (possibly a .cvs file or a .txt file) the values of that array. That way I can look at how my function is evolving over long times. Does anyone know how to do this?
If you need to see my code I can add it, but it's rather long so I've ommitted it for now.
EDIT:
I've tried using
for n = 1:n_max
(lots of stuff defining f at each n value)
if ~(mod(n,1000))
export(mat2dataset(f(:,:,n)))
end
end
which seems to convert f to the appropriate type of dataset and then exports it to a text file which is great. The only problem with this is that I seem to be writing over the same document for each n that I get. Is there a way that I can give each new file a new name without having to write a new line in my script for each n?

Answers (4)

Wayne King
Wayne King on 21 Jul 2014
Edited: Wayne King on 21 Jul 2014
You can certainly insert a write operation to catch every 1000 iterations by inserting an if statement on your counter.
Here is just a very simple example.
fid = fopen('test.txt','w');
for nn = 1:3000
x = nn;
if ~(mod(nn,1000))
fprintf(fid,'%4.2f\n',x);
end
end
Obviously this very simple example simply writes 1000.00, 2000.00, and 3000.00 to a text file because the value of the variable equals the value of the loop counter.
So basically you insert an if statement that catches when your counter hits 1000, 2000, etc.
You may actually want to just think about catching those matrices as elements of a cell array or something similar and handling the write operation (of just those matrices) after your simulation executes.
  1 Comment
sasha
sasha on 21 Jul 2014
Would this give a different name to every saved file? Also, I'm a little confused on how to use fprintf. All of the examples I've found print things out as columns. But my array is 201 by 201 and I want it printed out in that way. Not as a column. would that be possible?
Basically if I had a matrix:
A = [A1 A2 A3; A4 A5 A6; A7 A8 A9]
I would want it to print out in the txt file as something with 3 rows and 3 colums

Sign in to comment.


Chad Greene
Chad Greene on 21 Jul 2014
If you can keep your analysis in Matlab, saving to a .mat file tends to be a smoother process. The syntax is
save myfile
to save all the variable in your workspace or
save variable1 variable2 someothervariable
to save specific variables. If you prefer text files, dlmwrite can do it as well.
If you're looping some solution, say you're going through the loop 10,000 times, you can save every time n is divisible by 1000 with
for n = 1:10000
myVariable = 5 + n; % or whatever calculations you're doing
if mod(n,1000)==0
save(['solution_number',num2str(n),'.mat'],'myVariable');
end
end
  3 Comments
Chad Greene
Chad Greene on 21 Jul 2014
When n=1000, the ['solution_number',num2str(n),'.mat'] saves a .mat file called solution_number1000.mat. When n=2000, the ['solution_number',num2str(n),'.mat'] saves a .mat file called solution_number2000.mat and so on.
Chad Greene
Chad Greene on 21 Jul 2014
Edited: Chad Greene on 21 Jul 2014
You could apply the same concept to Wayne's solution if you want to write multiple text files with different names. Instead of his
dlmwrite('testfile.txt',x,'-append');
You could use
dlmwrite(['solution_number',num2str(n),'.txt'],x)

Sign in to comment.


Wayne King
Wayne King on 21 Jul 2014
Edited: Wayne King on 21 Jul 2014
If its just a matrix, then you can use dlmwrite() with the '-append' option that will simply append the data. By default, dlmwrite() will comma separate the elements of the matrix, but you can control that by specifying a different delimiter.
for nn = 1:3000
x = randn(4,4);
if ~(mod(nn,1000))
dlmwrite('testfile.txt',x,'-append');
end
end
Note that fopen() also has a 'a+' option that appends.
  1 Comment
sasha
sasha on 21 Jul 2014
I don't want to append the matrizes onto eachother. It ends up being a really really big matrix with the separate n value matrices not really separated from eachother. What I really would want in the above senario is 3 separate files with names like Variable_{n} with n replaced by the appropriate n value

Sign in to comment.


Wayne King
Wayne King on 22 Jul 2014
OK, so simply change the filename each time. In the following example I just create a file called Iteration1000.txt, Iteration2000.txt, etc and just write that 4x4 matrix.
for nn = 1:3000
x = randn(4,4);
if ~(mod(nn,1000))
filename = ['Iteration' num2str(nn) '.txt'];
dlmwrite(filename,x);
end
end
Thanks for accepting my answer if I have helped you.

Community Treasure Hunt

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

Start Hunting!