Receiving error "Unable to open csv file". What could be causing this issue?

22 views (last 30 days)
Hello, I'm trying to convert my .csv file into a table. I'm receiving the error above. It also says "Error: readtable(filename)" when converting the .csv file to a table. I've tried changing the name of the file or editing the file path but, I'm still getting the error. What could be the cause of this error?
%Opens file explorer to select .csv file
[filename, ~] = uigetfile("*.*");
%The data variable is just an example variable name
data = readtable(filename);
%Convert the table to a times table
T = table2timetable(data);
%Display summary of data so I can start manipulating the headers
summary(T);
  6 Comments
Star Strider
Star Strider on 9 Sep 2024
Use the which function to see if it is on your MATLAB search path. It may be necessary to use fullfile.
If it is, or once you have located it, use type, readlines or fileread to see what its contents are.
Stephen23
Stephen23 on 9 Sep 2024
Do NOT change folders just to read/write data files. This is slow and makes debugging harder.
Do NOT add folders to the MATLAB Search Path just to read/write data files. This pointlessly pollutes the search path with files, which slows down MATLAB. Similarly, do not keep your data files on the search path!
The reliable and efficient approach is to use absolute/relative filenames. FULLFILE helps with that.

Sign in to comment.

Accepted Answer

Govind KM
Govind KM on 9 Sep 2024
Edited: Govind KM on 9 Sep 2024
Hi Norma,
This error message indicates that the selected .csv file is not in the MATLAB path and is hence not accessible by the “readtable” function. To be able to read the file, you can either
  • Change the present working directory to the location of the selected file through the “cd” function
  • Add the folder containing the selected file to your MATLAB path through the “addpath” function
  • Provide the full location of the file to the "readtable" function
Sample code for these approaches is as follows:
Select the required file and get its filename and location:
%Opens file explorer to select .csv file
[filename, loc] = uigetfile("*.*");
Approach 1: Change directory:
%Change current working directory to file location
cd(loc);
data = readtable(filename);
Approach 2: Add to path:
%Add folder contatining selected file to MATLAB path
addpath(loc);
data = readtable(filename);
Approach 3; Provide full path to "readtable" function:
%Provide the full location of the file
data = readtable(fullfile(loc,filename));
You can refer to the documentation to understand more about the MATLAB path:
Hope this resolves the issue.
  8 Comments
Govind KM
Govind KM on 9 Sep 2024
I hadn't used AI for the answer, I was just listing out the possible approaches for the goal that I knew of and had verified at my end. I agree that I should have recommended the "fullfile" approach in my answer instead of simply listing the possible solutions. Will keep this in mind.
Govind KM
Govind KM on 9 Sep 2024
Edited: Govind KM on 9 Sep 2024
It would be difficult to understand why the header names are not showing up without any knowledge of the data being converted to a table. If your .csv file cannot be shared, perhaps you could share a small .csv file with mock data that has the same issue?

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!