[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: automated text file reading and array element extraction
From: |
Leo Butler |
Subject: |
Re: automated text file reading and array element extraction |
Date: |
Fri, 3 Dec 2010 17:55:50 +0000 (GMT) |
User-agent: |
Alpine 2.00 (DEB 1167 2008-08-23) |
On Thu, 2 Dec 2010, Michael Hadrien wrote:
< Hello eveyone,
< I'm a beginner in Octave, running Octave 3.2.4 on Ubuntu 10.10.
< What I'm trying to do is read numerical data in sequentially named text files
in two different directories, extract the second column from each file in both
< directories, subtract all the columns in one directory from all the columns
in the other directory, then create a new directory with a set of files
containing the
< first column (from any directory, they're constants) and the resulting
columns from the subtraction.
<
< So Directory A has:
< aaa_001.txt
< aaa_002.txt
< aaa_003.txt
<
< Directory B has:
< bbb_001.txt
< bbb_002.txt
< bbb_003.txt
<
< The files all look like this:
< 33 32
< 48 45
< 76 34
< 46 54
<
< The first column never changes.
< I want to take second columns from one directory and subtract them from all
the second columns in the other directory. Both directories have the same
number of
< files.
<
< This is the code I currently have:
<
< files=dir('*.txt'); #creates struct with all files
< filesarray = struct2cell(files); #converts struct array to cell array
< names=filesarray(1,:) #extracts first row of cell array (where all the file
names) and assigns to names
<
< for i=1:length(names)
< eval(['load ' files(i).name ' -ascii'])
< end
<
<
< I've repeated this for both directories. Now all the files have been loaded
and I can extract column 2 from each file individually using:
<
< aaa_001(:,2)
<
< But I want to be able to do this for all the files automatically and do the
subtraction operations.
< I'm not sure what to search or how to go about this. I've thought of maybe
creating two matrices with all the col 2 data from each directory on a
different matrix,
< but I don't know how to do that. Then I'd be able to subtract the matrices.
The next step would be extracting and writing each column to a new text file.
<
< Any help I can get would be greatly appreciated!
A general rule: if you are using eval, you are doing it wrong.
I would try something like:
filesA=dir('A/*.txt');
filesB=dir('B/*.txt');
for i=1:length(filesA)
filesA(i).data=load(fileA(i).name);
filesB(i).data=load(fileB(i).name);
endfor
Now you have your data loaded in memory and you can do what you
like. Since the data and filenames are associated, you can
create a new filename and write your transformed data to that
file in a mechanical way without resort to eval.
Leo
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.