MATLAB: Opening multiple text files and populating a matrix with the data

fopentextscan

Hi All,
I have multiple text files from signal processing analysis. There are 3 columns of data: Time, Voltage A (transmitter), Voltage B (receiver). The text files are created during tests with repeated signals; that being said the time column and Voltage A (transmitter) column are identical.
I would like to open these N text files and read the 3rd column of data; populate a matrix with the data; and then average the data to remove background noise. I believe I have correctly implemented the opening and reading of the text files, I am unsure on how to populate a matrix for averaging the signals outside the loop. The trial code I have been working on calls on 20 separate text files. So far I have:
path='C:\data\';
textFiles=dir([path '*.txt']);
for k = 1:length(textFiles)
textFilename = ['20110526-0001_' num2str(k) '.txt'];
%Open text file, 'rt' is for reading a text file only
fopen(textFilename, 'rt');
%Read file into 3 seperate variables
[Time, Transmitter, Receiver] = textscan(fid,'%d%d%d','Headerlines',3);
%Populate a cell array with data
data1=Time{i};
data2=Transmitter{i};
data3=Receiver{i};
%Close the text file
fclose(fid);
end
I prefer to have one output matrix of 22 columns: Time, Receiver A, Receiver B(1:20) after which I would take an average of Receiver B data at each timestep. As you can see above, I am getting somewhat lost trying to populate a matrix inside the loop.
Any help is appreciated. Nick

Best Answer

Your overall code is right. You need to correct a few syntax.
1. Do not use "path" as a variable name because it is a Matlab command.
2. Your fopen() need to return the fid
3. You need to understand the data format returned by textscan()
4. You need to make sure all your text files contain the same number of rows to make this code work.
Your code could be modified:
PathStr='C:\data\';
textFiles=dir([PathStr '*.txt']);
MyData=zeros(100,22); %assume data has 100 rows
for k = 1:length(textFiles)
textFilename = ['20110526-0001_' num2str(k) '.txt'];
%Open text file, 'rt' is for reading a text file only
fid=fopen(textFilename, 'rt');
%Read file into 3 seperate variables
Data = textscan(fid,'%d%d%d','Headerlines',3);
%Populate a cell array with data
MyData(:,k+2)=Data{3}; % put in the third row data
%Close the text file
fclose(fid);
end
MyData(:,1)=Data{1}; % put in data for time
MyData(:,2)=Data{2}; % put in transmitter data
There are still rooms for improvement. But, try to make this work first.