Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My directory contains 2550 text files. I want to read these files as separate array elements of the cell array file_contents. Reading one file at a time is easy i.e. 1.txt into file_contents{1} and so on. I want to create a loop that will read all these files into respective element of the file_contents cell array.

What I have tried:

The code I tried:

file_contents = {};

for i = 1:2551
    file_contents{i} = readFile('%d.txt', i);
end


Running the above code gives me the error:

error: fscanf: invalid stream number = -1


Here is the readFile function I am using:

function file_contents = readFile(filename)

fid = fopen(filename);
if fid
    file_contents = fscanf(fid, '%c', inf);
    fclose(fid);
else
    file_contents = '';
    fprintf('Unable to open %s\n', filename);
end

end
Posted
Updated 8-Jan-17 2:05am
Comments
Patrice T 8-Jan-17 7:24am    
And you have a question ?
Describe the problem.
Pratham Solanki 8-Jan-17 7:27am    
How do I read the multiple files as I intend to because the code I am trying is giving me the error I mentioned.

C++
file_contents{i} = readFile('%d.txt', i);

You are passing two parameters to the readFile function, but it only accepts the first one. So when you try to call fopen('%d.txt'); there is no such file, and the function returns -1. You then call fscanf(-1, ...) which fails as there is no such open file stream.

You need to change your readFile function to accept both parameters, and combine them to form a valid file name. You also need to check the value returned from fopen is not equal to -1, which indicates an error.
 
Share this answer
 
You are calling a function
readFile('%d.txt', i)
with 2 arguments, and that function is
function file_contents = readFile(filename)
.Notice any thing amiss?
+++++{round 2]+++++
Quote:
Yes! I am passing 2 arguments while the function only accepts 1.
Any suggestions as to how I may resolve it?

Are you kidding? It is your code, your function. First, ask yourself, what purposes those 2 arguments are, if they are really needed for the function to do the job, then add one more argument to the readfile() function, otherwise, get rid of the redundant one from the calling function. This is very basic.
Getting code from somewhere and hoping it to work out-of-the-box is getting you nowhere. The correct way is to read up on the relevant documentation and do it correctly:
1. Function Basics - MATLAB & Simulink[^]
2. Read data from text file - MATLAB fscanf[^]
2. Loop Control Statements - MATLAB & Simulink[^]
 
Share this answer
 
v3
Comments
Pratham Solanki 8-Jan-17 8:01am    
Yes! I am passing 2 arguments while the function only accepts 1.
Any suggestions as to how I may resolve it?
Peter Leow 8-Jan-17 8:05am    
solution updated.
You get the error because fopen(filename); fails and it fails because '%d.txt' do not magically transform to a filename.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900