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

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 件のコメント

per isakson
per isakson 2017 年 10 月 13 日
Spaces in names of folders and files used to cause trouble on Windows.
Nicolas Schmit
Nicolas Schmit 2017 年 10 月 13 日
If you still have problems, please provide the csv file.
Walter Roberson
Walter Roberson 2017 年 10 月 13 日
What does
!ls -1B
show when you are in the directory?
Note: the character after the '-' is the digit 1, not lower-case 'L'; also it is important to use capital B.
LM
LM 2017 年 10 月 13 日
Hi Nicolas Schmit, here is the csv file, i am still having problems.
Thanks
Jan
Jan 2017 年 10 月 13 日
Then be so kind and mention, which problems you have.
LM
LM 2017 年 10 月 13 日
編集済み: LM 2017 年 10 月 13 日
When i want to import this excel file into Matlab using readtable it gives me this error.
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.
Matthew Eicholtz
Matthew Eicholtz 2017 年 10 月 13 日
I just downloaded the csv file you provided, navigated to the appropriate directory in MATLAB, then used the following command without error:
T = readtable('equity_dataset_30_2.csv');
What are you doing differently?
LM
LM 2017 年 10 月 13 日
Oh ok, I don’t know. how did you navigate it to the appropriate directory maybe i am doing it wrong.
Matthew Eicholtz
Matthew Eicholtz 2017 年 10 月 13 日
Click on the "Browse for folder" icon circled in red below.
Then, pick the directory that your csv file is located in.
Matthew Eicholtz
Matthew Eicholtz 2017 年 10 月 13 日
編集済み: Matthew Eicholtz 2017 年 10 月 13 日
Alternatively, you can provide the fullpath of the file when you call the readtable function. So, for instance, if your file is in the C:\Downloads folder, you can use
T = readtable('C:\Downloads\equity_dataset_30_2.csv');
instead of
T = readtable('equity_dataset_30_2.csv');
LM
LM 2017 年 10 月 13 日
Ok, thank you so much i tried it again and it worked. However it did give me a warning though.
Walter Roberson
Walter Roberson 2017 年 10 月 13 日
T = readtable('equity_dataset_30_2.csv', 'HeaderLines', 1);
T.Properties.VariableNames = {'Date', 'AA', 'AI', 'AXP'};
Benjamin Robistow
Benjamin Robistow 2018 年 11 月 2 日
編集済み: Walter Roberson 2020 年 6 月 11 日
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 2018 年 11 月 2 日
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)

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2017 年 10 月 13 日

1 投票

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 件のコメント

LM
LM 2017 年 10 月 13 日
編集済み: LM 2017 年 10 月 13 日
Thank you, according to your m.code my file does bizarrely not exist. how do i make it exist?
Walter Roberson
Walter Roberson 2017 年 10 月 13 日
Is the file inside C:/users/Apple/Desktop/MATLAB/Individual Coursework/Q1 or inside C:/users/Apple/Desktop/MATLAB/Individual Coursework ?
We can see from the screen snapshot of your code that you have two assignments to fullFileName, one of which uses the fullfile() call that Image Analyst shows, and the other that assigns a specific string that we cannot see the end of in the snapshot.
LM
LM 2017 年 10 月 13 日
編集済み: LM 2017 年 10 月 13 日
The file is inside C:/users/Apple/Desktop/MATLAB/Individual Coursework/Q1.
Here is another screenshot
Walter Roberson
Walter Roberson 2017 年 10 月 13 日
Your screenshots are from a Mac, not Windows. You do not have a C: . You should remove the 'C:' from your file name.

サインインしてコメントする。

Neel Khakhar
Neel Khakhar 2020 年 6 月 11 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2020 年 6 月 11 日
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.

サインインしてコメントする。

Nelly Moulin
Nelly Moulin 2022 年 2 月 8 日

0 投票

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 件のコメント

Jan
Jan 2022 年 2 月 8 日
It is a bad idea to add folders to Matlab's PATH only to import data files. Prefer to use full path names instead for accessing the files. This avoids troubles, if files with the same name occur in different subfolders. With adding the folders to the PATH and using the file names only, you have no control which file is opened.
Ashishkumar Gupta
Ashishkumar Gupta 2023 年 1 月 5 日
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 2023 年 1 月 5 日
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:

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

質問済み:

LM
2017 年 10 月 12 日

コメント済み:

2023 年 1 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by