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

127 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2024-9-9,13:32
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 2024-9-9,17:35
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.

请先登录,再进行评论。

采纳的回答

Govind KM
Govind KM 2024-9-9,13:31
编辑:Govind KM 2024-9-9,13:41
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 个评论
Govind KM
Govind KM 2024-9-9,18:25
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 2024-9-9,18:28
编辑:Govind KM 2024-9-9,18:30
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?

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by