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

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

Hi @Norma, please mention the exact error you are facing for better understanding of the problem. If possible, please the share the CSV as well using the paper clip icon.
For privacy reasons, unfortunately I can not share the csv file. The most I can give is the code above. I have troubleshooted enough to know that it's not the csv file itself because I have a separate project that processes the same files correctly. I just wanted to know if there are any instances that can cause a csv file not to convert to a table? If so, is it possible to receive a list so I can check off the potential causes?
The specific errors I'm getting are "Unable to open file 'xxx.xx.xx.csv'" and "data = readtable(filename);"
The filename you want to open really is xxx.xx.xx.csv ?
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.
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

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

Hello, thank you so much! Approach 3 worked for me but it didn't generate the header names when converting it to a times table. How would I edit it to make it do that?
Update: I realized that the reason it wasn't working is because I placed the data inside a folder in the MATLAB path. It should be fine now.
  1. Do NOT change folders just to read/write data files. This is slow and makes debugging harder.
  2. 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!
  3. This is the reliable, efficient, recommended approach. Use it.
Just because AI says it does not mean that it is a good approach that should be recommended on this forum.
I wasn't using AI. I'm trying to allow a user to select which file folder they want to grab data from since I'm dealing with two different types of data and then convert the data into a table. I created the folders in the MATLAB Path thinking it would be easier to keep all the information in one location being the project folder. I also took this approach because MATLAB won't let me grab data from anywhere other than the MATLAB path. It was to keep multiple files of different data organized in their separate folders. I did not know that was not an ideal approach. I'm new to learning how to use MATLAB so my knowledge from other coding languages is how I tend to approach my coding in MATLAB.
I apologize, I misunderstood what you were referring to since I commented under the answer prior to your comment. I thought you were responding to my comment. I thought you were saying that having a file folder inside a project folder, wasn't a good approach. I tried using FULLFILE to access the data but, it doesn't seem to notice the header names when I convert it to a times table so, I'm going to have to troubleshoot some more.
"I tried using FULLFILE to access the data but, it doesn't seem to notice the header names when I convert it to a times table..."
FULLFILE does not know anything about file data. It just joins text (path names and filenames) together.
"...so, I'm going to have to troubleshoot some more."
Show us the code you tried.
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.
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)

Categories

Products

Release

R2019a

Asked:

on 9 Sep 2024

Edited:

on 9 Sep 2024

Community Treasure Hunt

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

Start Hunting!