append/save same variable with updated values into .mat file(row-wise)
조회 수: 15 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Guillaume
2015년 12월 18일
The proper way to do what you want is to use matfile which lets you read and write to part of variables in a mat file.
filename='z.mat';
m = matfile(filename, 'Writable', true); %Note: writable is true by default IF the file does not exist
for row = 1:10 %why do you call it col when you're asking for rows?
%out = zeros(1,1000000); %completely unnecessary
x = randn(1,1000000);
out = x.^2; %whatever you put into out is overwritten here anyway
m.out(row, 1:1000000) = out;
end
clear m;
댓글 수: 1
추가 답변 (4개)
Thomas
2012년 3월 22일
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
댓글 수: 3
Thomas
2012년 3월 22일
True.. My bad..
http://www.mathworks.com/help/techdoc/matlab_prog/f10-61206.html#f10-61260
Note Saving with the -append switch does not append additional elements to an array that is already saved in a MAT-file
Malcolm Lidierth
2012년 3월 22일
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) ??
댓글 수: 0
Wilbert Sequeira Sandoval
2015년 5월 27일
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
Leigh Martin
2015년 12월 18일
편집: Leigh Martin
2015년 12월 18일
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))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!