Receiving error "Unable to open csv file". What could be causing this issue?
22 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 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.
Akzeptierte Antwort
Govind KM
am 9 Sep. 2024
Bearbeitet: Govind KM
am 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 Kommentare
Govind KM
am 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
am 9 Sep. 2024
Bearbeitet: Govind KM
am 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?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!