Borrar filtros
Borrar filtros

Why do i get an error when using readtable saying cannot open file

210 visualizaciones (últimos 30 días)
LM
LM el 12 de Oct. de 2017
Comentada: Image Analyst el 5 de En. de 2023
Error using readtable (line 198) Unable to open file ‘equity_dataset_30_2.csv’.
I have made sure i have used the file name correctly and imported the data. I have tried fixing the error but cannot get rid of it.
  14 comentarios
Benjamin Robistow
Benjamin Robistow el 2 de Nov. de 2018
Editada: Walter Roberson el 11 de Jun. de 2020
Try using the following it will get the whole path and make the selection a little more dynamic.
curr_dir = dir;
[filename, pathname] = uigetfile('*.csv');
T = readtable(strcat(pathname,filename));
cd(curr_dir);
This will solve the issue incase you are trying to use a file that is nested or located somewhere that isn't in the current working directory
Walter Roberson
Walter Roberson el 2 de Nov. de 2018
You cannot cd to the result of dir() . You can cd to the result of pwd(), but there is no point in doing so because using uigetfile() and readtable() do not change the working directory.
[filename, pathname] = uigetfile('*.csv');
T = readtable(strcat(pathname,filename));
There is no promise that the path returned by uigetfile() will end in a terminator, so you should not use strcat() between the parts: use fullfile(pathname, filename)

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 13 de Oct. de 2017
It's probably that unneeded 'format' option. It works fine without it. Try this, which is more robust than what you have:
baseFileName = 'equity_dataset_30_2.csv';
folder = 'C:/users/Apple/Desktop/MATLAB/Individual Coursework/Q1';
fullFileName = fullfile(folder, baseFileName)
if exist(fullFileName, 'file')
% File exists. Read it into a table.
t = readtable(fullFileName)
else
% File does not exist. Warn the user.
errorMessage = sprintf('Error: file not found:\n\n%s', fullFileName);
uiwait(errordlg(errorMessage));
return;
end
  4 comentarios
LM
LM el 13 de Oct. de 2017
Editada: LM el 13 de Oct. de 2017
The file is inside C:/users/Apple/Desktop/MATLAB/Individual Coursework/Q1.
Here is another screenshot
Walter Roberson
Walter Roberson el 13 de Oct. de 2017
Your screenshots are from a Mac, not Windows. You do not have a C: . You should remove the 'C:' from your file name.

Iniciar sesión para comentar.


Neel Khakhar
Neel Khakhar el 11 de Jun. de 2020
Try using path(pathdef) and cd to desired path. If the file is also shared on a server, it might be locked/lost by source control. I faced a similar issue intermittently while working on my project. It helped.
  1 comentario
Image Analyst
Image Analyst el 11 de Jun. de 2020
Her screenshot shows she did call cd() to the folder where she thought the CSV file lived.
It's possible that the file just isn't there or locked for some reason. Maybe someone else is actively editing it??? With Windows, it will tell you if someone else has it locked and offer to let you open a read-only copy.
One thought is that she misspelled Individual as Indvidual when she called cd(), but that should have thrown an "Error using cd" error, so maybe it is actually misspelled.
Another thought is that in readtable() she used 'equity_dataset_30-2.csv' while in the comment she said that she was planning on reading in 'equity dataset 30.csv'. Not sure which of those two filenames is the actual, correct filename. Should it have underlines and a -2, or not??? But that is another discrepancy.

Iniciar sesión para comentar.


Nelly Moulin
Nelly Moulin el 8 de Feb. de 2022
I had the same problem. In my case, it was solved by adding the subfolder to Matlab path.
To do this, right click on the folder in the document window in Matlab. Then click on "Add to Path\this folder and the subfolders".
I hope this message can help :)
  3 comentarios
Ashishkumar Gupta
Ashishkumar Gupta el 5 de En. de 2023
Hello All,
I am trying to import files with some weird extension like '.rec1066-5(4244)','.rec1067-5(4244)'.
The extension is taken care of changing it within the code to '.txt'
But some files are getting imported while some files with similar name and same type are NOT getting imported.
the error is:
Error using readtable
Unable to find or open 'C:\Users\ashis\OneDrive\Desktop\WS\a1.txt'. Check the path and filename or file permissions.
Please help me out!! Thanks!!!
Image Analyst
Image Analyst el 5 de En. de 2023
You need to parse the filename properly and then use sprintf and fullfile to construct a new filename that can be used when you call movefile.
Are you sure that readtable does not work with the original filename?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by