How to process and save a matrix with header. Help much appreciated

3 views (last 30 days)
Hi. I am trying to solve the following problem: I have several input files, which consists of matrices with headers in ASCII format. For example:
ncols 1000
nrows 1000
xllcorner 480191
yllcorner 7110654
cellsize 2
nodata_value 0
13 34 12 55 65 68
15 64 23 23 44 52
11 21 88 13 43 11
I can extract data from the files with
eval(['x = importdata(files(i).name)']);
...which results in a file in my workspace with matrix data (x.data) and header information (x.textdata). I want to divide x.data with the sum of the matrix ( sum(x.data(:))), and then save a new file with the same header, but with the new numbers that are divided with the sum. I have 500+ files, so I have to have it in a for-loop. Does anyone have an idea as how to do it? This is my attempt so far, but the code crashes at "contribution_percent"
clear;
%read files from folder
files = dir('H:\testfolder\*.ASC');
%for-loop with same number of iterations as number of files.
for i=1:length(files)
%files are made into a vector
eval(['x = importdata(files(i).name)']);
contribution_percent = (x.data/sum(x.data(:)));
end
Thanks a lot.

Answers (1)

Walter Roberson
Walter Roberson on 15 Aug 2012

Community Treasure Hunt

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

Start Hunting!