Reading a all the rows in a CSV file
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.
Accepted Answer
More Answers (1)
...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.
Categories
Find more on Search Path 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!
