append/save same variable with updated values into .mat file(row-wise)
Afficher commentaires plus anciens
What is the syntax for appending same variable with different values to an existing .mat file? When I use -append, I end up replacing the values!
Example:
for col = 1:10
out = zeros(1,1000000); %reset out to zero vector
x=randn(1,100000);
out=x.^2;
if col == 1
filename='z.mat';
save(filename,'out','-v7.3'); % Write to MAT file
else
save(filename,'out','-v7.3','-append');
end
end
After running the above code, I have the replaced values instead of appending the updated/current values in out into the .mat file after every loop iteration.
Everytime, I iterate through the for loop, I want to save current values in the variable out(1x1000000) into the .mat file row-wise. At the end, I should have (10x1000000) matrix in z.mat and my variable out should hold only 1x1000000 vector during each iteration at a time.
How can I achieve this?
What am I missing?
Réponse acceptée
Plus de réponses (4)
Thomas
le 22 Mar 2012
You are always overwriting the same variable 'out' in each of the iterations..
for col = 1:10
out = zeros(1,1000000); %reset out to zero vector
x=randn(1,1000000);
out(col,:)=x.^2;
if col == 1
filename='z.mat';
save(filename,'out','-v7.3'); % Write to MAT file
else
save(filename,'out','-v7.3','-append');
end
end
Malcolm Lidierth
le 22 Mar 2012
0 votes
See the Project Waterloo MAT-file Utilities, they will let you "grow" a version 6 MAT-file entry using AppendVector and AppendMatrix
I assume you have a reason not to use rand(10,100000) ??
Wilbert Sequeira Sandoval
le 27 Mai 2015
0 votes
If what you want is to grow a matrix you have in the file then you need to first load the one in the file, say the matrix's variable name is A.
load(filename,'A');
Now if you want to append as rows, you do:
A=[A;B];
If you want to append as columns:
A =[A:B];
Where B is what you want to append(it must have the same number of columns as A if you're appending it as rows and the same number of rows if you're appending it as columns). It will be slow but it will work. Then at the end you use -append to save it to the file again(which will override the old A with the updated version).
1 commentaire
Leigh Martin
le 18 Déc 2015
Modifié(e) : Leigh Martin
le 18 Déc 2015
You can load all variables at once and create a concatenated array, but this doesn't work if the variables are too big to load into memory (which is presumably why you would want to do this in the first place). If you don't need the array saved as one big array, here's a useful work-around:
% this code saves variables named array_part1, array_part2... from your data
for i = 1:10
data = i*ones(1,5); % create some fake data
eval(sprintf('array_part%i = data', i))
save('file.mat', sprintf('array_part%i',i), '-append')
clear(sprintf('array_part%i',i))
end
you can then load whatever part you like using
load('file.mat', sprintf('array_part%i',i))
eval(sprintf('array = array_part%i', i))
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!