I try to import one single point from 20 different mat file and save those point into a mat file without overwrite it. However my code keep overwritting it and only display the last value. Here is my code
for k = 1:20
myfilename = sprintf('Image%d.mat', k);
a = load(myfilename);
newimg = (a.img -273);
% The point of plants
i = 100;
j = 84;
% Take the average of itself and all four of its neighboring
% values average each point in array
newimg(i,j) = 0.2*(newimg(i+1,j) + newimg(i-1,j) + newimg(i,j+1) + newimg(i,j-1) + newimg(i,j))
val = newimg(i,j)
newa = length(k);
save('newa.mat','-append','val');
end

댓글 수: 1

Rather than save your variable to the .mat-file on each iteration, simply construct an array with those values, and then save the array once after the loop, something like this:
N = 20;
mat = NaN(1,N); % preallocate
for k = 1:N;
...
mat(k) = val;
end
save(..., 'mat')

댓글을 달려면 로그인하십시오.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 1일
편집: Azzi Abdelmalek 2016년 5월 1일

0 개 추천

You are using the same variable name. If you want to append your new data, you have to store them in a new variable name. But its better if you create a cell array then each update of your data, the size of your cell array will increase
For example
for k=1:3
a{k}=randi(4,4);
save('file','a','-append')
end

댓글 수: 3

Stephen23
Stephen23 2016년 5월 1일
There is no need to use a cell array, a simple numeric array will work perfectly, and is simpler. Some preallcoation would be nice though!
Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 1일
That depends on the size of the new data
Ki Lung Chan
Ki Lung Chan 2016년 5월 1일
thank you! I got this

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2016년 5월 1일

댓글:

2016년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by