help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: automated text file reading and array element extraction


From: William Krekeler
Subject: RE: automated text file reading and array element extraction
Date: Mon, 6 Dec 2010 16:39:40 +0000

Michael,

 

I would strongly encourage you to not use eval. Leo was right, I should have removed it in my suggested update of your code. You aren’t seeing a performance penalty because you are only opening 4 very small files and manipulating small data sets.

 

Bill Krekeler
Electro-Optics Engineer
CET, LLC
address@hidden

 

From: Michael Hadrien [mailto:address@hidden
Sent: Friday, December 03, 2010 8:11 PM
To: William Krekeler; Leo Butler; Octave
Subject: Re: automated text file reading and array element extraction

 

Bill & Leo: Thank you for your help! I can use this to keep cranking out my program.

 

I tinkered around with the code and managed to make it work last night at some ungodly hour :)

 

for d = 1:filenum %d=the number of files in directory

  s = ['load ref_' int2str(d) '.txt']; %load sequential text files

  eval(s);           %display files

end %loaded files

 

row = rows(ref_1) #number of rows in one file (each row = diff wavelength)

 

A = [1:row]; %create matrix A with rows = num of wavelengths (waves)

A = A'

 

for d = 1:filenum         %d = num of files in directory

  c = ['ref_' int2str(d)]; %call all text file variables in dir

  eval(c); %displayed variables

  x = ['ref_' int2str(d) '(:,2)'];   %extract int col from each file

  eval(x); %extracted columns

  col = eval(x);

  A = [A col] %add each file col to matrix A

end

 

 

--Michael

 

On Fri, Dec 3, 2010 at 9:55 AM, Leo Butler <address@hidden> wrote:



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="">  filesB(i).data="">  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.

 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]