Load multiple text files one after another
Show older comments
Hello,
I have 30 text files with the format "Data1.x_pre" and "Data1.x_post" with x being numbered from 1-30. I want to load in and run through one MATLAB script. Currently, I am loading all of 30 text files separately which is quite tediuos. What I basically want to do is load the first text file ("Data1.1_pre"), run it through the script and collecting the output in a new matrix "Alldata". Then, I'd like to close the first text file and continue with the second file ("Data1.1_post") and insert the output into matrix "Alldata". After that: Closing "Data1.1_post", loading next text file ("Data1.2_pre") and so on.
My section to load and read these text files:
textFilename = ['Data1.1pre.txt'];
fid = fopen(textFilename);
block = 1;
newLine = fgets(fid);
while ~feof(fid);
EEGData{1,q}(1,block) = textscan(fid, '%f %f %f %f', 'CollectOutput', true);
fgets(fid);
block = block + 1;
end
Is there a way to solve these two problems (reading all text files and collecting all the outputs into one common matrix) with only little changes to my current code?
Thank you in advance!
Accepted Answer
More Answers (2)
Pinga
on 19 Sep 2014
0 votes
6 Comments
per isakson
on 20 Sep 2014
Edited: per isakson
on 20 Sep 2014
- Your "answer" should have been a comment to my answer, not a new answer.
- Did you read the FAQ entry, How can I process a sequence of files?. Why not use that approach?
- "data-relative" functions"   what does that refer to?
- I don't see the rational behind the cell array EEGData, why not use something simpler
- What is q in EEGData{1,q}(1,block)?
- The while-loop looks unnecessarily complicated to me
- "function ... telling MATLAB to clear the workspace and to start again"   With my words that means "clear, goto". AFAIK: there is no such function.
Pinga
on 21 Sep 2014
per isakson
on 21 Sep 2014
Edited: per isakson
on 21 Sep 2014
The error message is about
exist( textFileName, 'Data1.1pre.txt' )
There is a problem with the syntax of the function, exist.   I think it pays off to read the documentation carefully. If you find it difficult to understand run examples. There are many in the Matlab documentation.
Doc says that the function, exist, returns a whole number.
Doc says: if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true.
Matlab allows all sorts of obscure constructs, e.g.
>> if 't', disp('true'), else, disp('false'), end
true
>> if 'false', disp('true'), else, disp('false'), end
true
>> if 17, disp('true'), else, disp('false'), end
true
IMO: Do not start using constructs like these and you will make fewer mistakes in the future.
If your data files are in the Matlab search path, the preferred syntax is
if exist( textFileName, 'file' ) == 2
if exist( fullfile( folder_name, textFileName ), 'file' ) == 2
per isakson
on 21 Sep 2014
Edited: per isakson
on 21 Sep 2014
- Do you have the data files in a special folder?
Pinga
on 22 Sep 2014
per isakson
on 23 Sep 2014
I've added a working function to my answer.
Guillaume
on 21 Sep 2014
To format your filenames:
for filenumber = 1:30
for postpre = {'post', 'pre'}
filename = sprintf('Data1.%d_%s.txt', filenumber, postpre{1});
if exist(filename, 'file')
%open file and process
else
%report error
end
end
end
if the for postpre = ... looks too weird for you, you can replace it with
postpre = {'post', 'pre'};
for postpreindex = 1:2
filename = sprintf('Data1.%d_%s.txt', filenumber, postpre{postpreindex});
14 Comments
per isakson
on 21 Sep 2014
IMO: sprintf makes more readable code compared to concatenation. I make fewer mistakes when using sprintf.
Pinga
on 22 Sep 2014
per isakson
on 22 Sep 2014
Edited: per isakson
on 22 Sep 2014
fopen does not issue a warning or error when it fails, it returns -1, which later causes the error you see.
An experiment
>> fid = fopen('abcd.xyz');
>> fid
fid =
-1
where abcd.xyz does not exist.
I guess there is some problem with the value of filename and that the value of fid is -1.
per isakson
on 22 Sep 2014
Here are some links on debugging in Matlab
Pinga
on 22 Sep 2014
Image Analyst
on 22 Sep 2014
How did you do that? Evidently the file just plain does not exist. Are you sure you used fullfile() and prepended the folder?
filename = fullfile(folder, baseFileName);
Check with exist
if ~exist(filename, 'file')
errorMessage = sprintf('This file does not exist:\n%s', filename)
uiwait(warndlg(warningMessage));
end
per isakson
on 22 Sep 2014
Edited: per isakson
on 22 Sep 2014
Professional programming requires that one asserts that fid>=3 or something.
filespec = fullfile( folder_spec, filename );
[fid,msg] = fopen( filespec );
assert( fid >= 3, ...., msg )
I usually approach the problem something like
sad = dir( fullfile( folder_spec, 'Data*.txt' ) ) ;
for jj = 1 : length( sad )
filespec = fullfile( folder_spec, sad(jj).name );
fid = fopen( filespec );
etc.
end
Pinga
on 23 Sep 2014
per isakson
on 23 Sep 2014
I assume that the three first lines of the text-files looks something like
colh1, colh2, colh3, colh4
11,12,13,14
21,22,23,24
now I see that there are 20 columns. Proposal: post the12 first lines of one text-file.
Guillaume
on 23 Sep 2014
Pinga, I'm not sure why you accepted an answer that doesn't seem to answer your question.
Anyway, take time to understand the code I gave in my answer, do not just paste it. The two comments:
%open file and process
%report error
were intended for you to replace with whatever you intended to do.
As it is, your code does nothing useful. Your copy of my loop generates filename, test if they exist and then does nothing but loop back. You then start your loop (which only has one iteration), try to open the last file generated by my loop. That file may not exist since you didn't do anything with the %report error line.
Here's what you should have done:
for filenumber = 1:30
for postpre = {'post', 'pre'}
filename = sprintf('P4.%d_%s.txt', filenumber, postpre{1});
if exist(filename, 'file')
%open file and process
fid = fopen(filename);
block = 1
newLine = fgets(fid); %did you mean fgetl(fid) ?
while ~feof(fid)
EEGData{filename}(block) = textscan(fid, '%f %f %f %f', 'CollectOutput', true);
fgets(fid);
block = block + 1;
end
else
%report error
fprintf('File %s does not exist.\n', filename);
end
end
end
Pinga
on 23 Sep 2014
Guillaume
on 23 Sep 2014
As I said, take time to understand what the code does. If necessary, use the debugger to step through it and look at the states of the variable.
The EEG line does not work because I made a mistake. filename was meant to be filenumber. It still wouldn't have worked properly though because I'd forgotten about the postpre loop. Use a counter instead, same as you've done with block to iterate into your EEG cell array or have a 2-d array based on filenumber and a post pre counter, e.g:
postpre = {'pre_all', 'post_all'};
for filenumber = 01:30
for postpreindex = 1:2
filename = sprintf('Ddata1.%d%s.txt', filenumber, postpre{postpreindex});
if exist(filename, 'file') %open file and process
fid = fopen(filename);
block = 1
newLine = fgets(fid); %did you mean fgetl(fid) ?
while ~feof(fid)
EEGData{filenumber,postpreindex}{block} = textscan(fid, '%f %f %f %f', 'CollectOutput', true);
fgets(fid);
block = block + 1;
end
else
%report error
fprintf('File %s does not exist.\n', filename);
end
end
end
Having a loop of the form:
for postpre = {'pre_all'}
is pointless. You're just iterating over one element. It's the same as writing:
postpre = 'pre_all'
per isakson
on 24 Sep 2014
"which says "Invalid file identifier"   I guess the problem is the format string in
filename = sprintf('Data1.%d_%s.txt', filenumber, postpre{1});
The name of the file, which you uploaded is
Data1.01_pre.txt
To get the leading zero, i.e .01_ not .1_, the format string should be
'Data1.%02d_%s.txt'
Pinga
on 26 Sep 2014
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!