Reading a all the rows in a CSV file
4 views (last 30 days)
Show older comments
Hi,
I have written the following code to read the csv file. But How can I modify to read all the rows in both columns. At the moment it only reads the first row of both columns. Also, I need to read multiple .csv files. (See the screenshot below)
clear all
folder='C:<path>Desktop';
fid = fopen('Test2.csv');
out = textscan(fid,'%s%f$f','delimiter',',');
fclose(fid);
Can somebidy advice me to improve this?
Thanks in adavance.
0 Comments
Accepted Answer
dpb
on 22 Aug 2014
out = textscan(fid,'%s%f$f','delimiter',',');
What's the '$f' in the format string intended to do? Eliminating it should solve the problem; as is, there's a failure after the second value.
For multiple files, see the FAQ at http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
I strongly recommend the dir solution over the alternatives.
4 Comments
dpb
on 22 Aug 2014
Didn't have anything to do with it since you hardcoded a filename in the fopen statement.
What did/does
[fid,msg]=fopen('014501.csv')
return? The error message will tell you (probably) "file not found" or the like which means it's either a typo and you don't actually have a file of that specific name or it's not in a directory on the matlabpath or in the working directory. Other causes are possible, but that's the most likely.
As I recommended before, if you have many of these to process, then
d=dir('*.csv');
for i=1:length(d)
fid=fopen(d(i).name);
...
will let you process them all sequentially w/o having to code up the names at all. Of course, you need to do the dir call on the right subdirectory to find them.
More Answers (1)
dpb
on 22 Aug 2014
Edited: dpb
on 22 Aug 2014
...I ran the code with 020101.csv file, the fid produced was 9 but when I ran the same code after changing the same code to 014501.csv, the fid was -1. I have no idea why?
>> help fopen
fopen Open file.
FID = fopen(FILENAME) opens the file FILENAME for read access.
FILENAME is a string containing the name of the file to be opened.
...
FID is a scalar MATLAB integer valued double, called a file identifier.
You use FID as the first argument to other file input/output
routines, such as FREAD and FCLOSE. If fopen cannot open the file, it
returns -1.
So, the file wasn't opened.
As Per says, the path string certainly looks funny unless you did substitute a real path in there manually. ( ADDENDUM I see you hardcoded a file name and so the path string/variable wasn't used. That means, of course, that the file must exist in the working directory or on the matlabpath variable to be found).
To see what the actual failure was in more detail use the optional return message to see just what the error was...
...
[FID, MESSAGE] = fopen(FILENAME,...) returns a system dependent error
message if the open is not successful.
...
One should programmatically, always check that the returned FID value is valid before continuing.
ADDENDUM 2 That you had a returned FID of 9 indicates you've got a lot (like 6) file handles hanging around. That's not necessarily wrong but likely indicates you're not religiously closing files when done with them. Matlab will being with FID 3 for file i/o as 1 and 2 are preassigned console and standard error.
0 Comments
See Also
Categories
Find more on Low-Level File I/O 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!