|
From: | William Krekeler |
Subject: | RE: automated text file reading and array element extraction |
Date: | Fri, 3 Dec 2010 15:21:46 +0000 |
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 (all file names) and assigns to names for
i=1:length(names) eval([ sprintf( '
files(%d).array = load( files(%d).name, ''-ascii'');',i,i) ]
) end X = struct2vec( files(:).array ); Xreduce = X( :, [2:2:size(X,2)] ); % only column 2 data from
each file Use the following function to convert to a matrix. function [X] =
struct2vec(varargin) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % function [X] =
struct2vec(varargin) % Author: William
Krekeler % Date : 20070713 % version: 1 % Synopsis: convert
structure array to a multidimensional matrix % % Returns: matrix x % % Variables: % X = data
vector % References: % See also: % Additional Notes: % this code
is useful for converting APMReadData output % Example Call: % DPATH = 'C:\some_path'; % DFILE0='some_data.txt'; % data0 = readYourDataProprietary(fullfile(DPATH,DFILE0)); % x =
struct2vec(data0.structureArray); % plot(x); % Copyright William
Krekeler 2010, you may distribute and modify, you may not charge for or close
source this code without my explicit permission. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% X = cell2mat(varargin); Bill Krekeler From: Michael Hadrien
[mailto:address@hidden 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! I've
attached example files (files beginning with aaa go in one directory and those
beginning with bbb go in another). Thank
you! --Michael |
[Prev in Thread] | Current Thread | [Next in Thread] |