summing every twelve files from multiple .mat files

조회 수: 2 (최근 30일)
FeAde
FeAde 2015년 4월 6일
댓글: FeAde 2015년 4월 6일
Hi everyone,
I am new to Matlab. This may look trivial. I have thousands .mat matrix files of size K(180, 360). I want to do two things:
  1. Read these files and summing every twelve files
  2. Clip the result from size (180, 360) to something like (100,100)
  3. save the results
This is what I have done:
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.mat'));
Total = zeros(180,360);
N=length(Files);
for k=1:N
ppm.Frame=load(Files(k).name)); % read the files
while k<= 12;
Total = Total + pp.Frame % do the summing of first twelve files here
k = k+1;
else
k=0;
% How do I resize the result here?
save(Total)
clear Total
end
Help would be highly appreciated.
  댓글 수: 1
Jan
Jan 2015년 4월 6일
What exactly is "K(180, 360)"? How can files be added? Do you mean that the MAT-files contain a variables called "K", which has the size [180 x 360]? And you want to create the sum over these values? How is "every 12 files" defined? In alphabetical order? How should the clipping be applied exatcly? Do you want to remove the right, left, upper, lower rows and columns?

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

채택된 답변

Jan
Jan 2015년 4월 6일
편집: Jan 2015년 4월 6일
I've formatted your code properly. As soon as you do so, you can see, that there cannot be an else ibn a while loop. The for loop counter k should not be modified inside the loop - you will see a corresponding warning in the editor.
The code contains more problems, e.g. you read the file into "ppm.Frame", but try to access ""pp.Frame". I guess, you want something similar to this:
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.mat'));
N = length(Files);
count = 0;
for iFile = 1:N
if count == 0
Total = zeros(180,360);
end
count = count + 1;
FileData = load(fullfile(dirname, Files(iFile).name)); % read the files
Total = Total + FileData.Frame;
count = count + 1;
if count == 12
% It is unclear how you want to "clip" Total.
Total = ??? Perhaps resize()
% Or: Total = Total(1:100, 1:100, :);
save(Total)
count = 0;
end
end
  댓글 수: 1
FeAde
FeAde 2015년 4월 6일
Hi Jan, Thanks s much for your response:
Actually the files are 30min precipitation values represented in a matrix. Want to accumulate them to 6 hr (therefore 12 variable in a file). The global coverage of K(180,360) where this is Y and X (long and lat).
The date is like this:
2011060100000, 2011060100300, 2011060101000, .
. . etc
where
This is Year:MM:dd:hrhr:minminmin
The files are in ascending order in the folder. I hope the function would pick each file(s) (i.e. first 12) to be the first 12 in the folder? Or is there a way to call the date as a step or sequentially?
can I do something like this for clipping to get(Total(100,100):
Total(80:end,:)=[]; Total(:,260:end)=[]; Total(:,1:80)=[];
save(Total)
Thanks a lot!!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by