Receiving error "Unable to open csv file". What could be causing this issue?
조회 수: 22 (최근 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
Stephen23
2024년 9월 9일
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
2024년 9월 9일
편집: Govind KM
2024년 9월 9일
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
2024년 9월 9일
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
2024년 9월 9일
편집: Govind KM
2024년 9월 9일
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!