Error while using readtable command in MATLAB

175 vues (au cours des 30 derniers jours)
Ahmad Bilal
Ahmad Bilal le 20 Mar 2018
Hi I have one question. I have downloaded one csv template file with .csv extension from Internet. Then I simply converted this .csv file into .dat extension.
filename = fullfile('Desktop','ABC','breslow.dat') %here breslow.dat is my file
Then i try to execute this command :
T =readtable(filename)
when i execute this it shows an error such as:
Error using readtable (line 198)
Unable to open file 'Desktop\ABC\breslow.dat'.
Can anybody help me in this regard ?? Thank you
  1 commentaire
Luciano Nascimento
Luciano Nascimento le 27 Déc 2022
redatable precisa ser aberto com o caminho do diretorio + file. neste exemplo o que está pedindo para abrir é o arquivo somente sem o diretorio completo, ai da erro.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 21 Mar 2018
Try it this way:
folder = 'C:\Users\Bilal Ahmad\Desktop\ABC';
fullFileName = fullfile(folder, 'breslow.dat')

Plus de réponses (5)

Jan
Jan le 20 Mar 2018
This does mean, that there is no such file. Note that the relative path 'Desktop\ABC\breslow.dat' is interpreted relative to the current directory as replied by the cd command. Most likely this current directory is not what you expect it to be. Prefer absolute path names to avoid such problems.
  5 commentaires
Steven Lord
Steven Lord le 5 Jan 2023
You haven't showed us the code that creates the variable X that you pass into readtable on line 45 of your trail.m file. But unless you have a folder named MATLAB_scripts_Ashish inside another folder named MATLAB_scripts_Ashish (which would be your current folder) the file you have circled in green in the Current Folder window in your screen shot is not the file you're telling your trail script to read on line 45.
Even if that is the file you intended your code to read, the file in the Current Folder does not have the extension .txt. Its extension is "rec1066-2(4244)".
Image Analyst
Image Analyst le 6 Jan 2023
We know from the files you attached in your other question that all your data was in a single line, instead of the first line being column headers and the data being below that, meaning that it was trying to make some columns (fields) names from numbers. You can't do that. You can have a table field called "mean" or "data" or whatever but you can't have a field called "-0.542" or "#####".

Connectez-vous pour commenter.


Ahmad Bilal
Ahmad Bilal le 20 Mar 2018
Modifié(e) : Walter Roberson le 21 Mar 2018
Hi, I have already checked that my breslow.dat is in the specific folder and it still shows the following error.
>> cd 'C:\Users\Bilal Ahmad\Desktop\ABC'
>> filename = fullfile('Desktop','ABC','breslow.dat')
filename =
'Desktop\ABC\breslow.dat'
C= readtable(filename)
Error using readtable (line 198)
Unable to find or open 'Desktop\ABC\breslow.dat'. Check the path and filename or file permissions.
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', 0, 'ReadVariableNames', true, 'Format', ''
It is still showing the error. Can you help me in this regard ??
  4 commentaires
Stephen23
Stephen23 le 21 Mar 2018
Modifié(e) : Stephen23 le 21 Mar 2018
@Ahmad Bilal: read Jeremy Hughes' comment again: the path you are searching for does not exist because you cd to a directory, and then use fullfile to build a relative path that does not exist in that location. If you have cd-ed to the directory ABC then all you need is
filename = 'breslow.dat';
However you should avoid cd completely, because it just slows code down and makes debugging harder. As the others have already written: use an absolute filepath, just as Image Analyst already showed you.
Ahmad Bilal
Ahmad Bilal le 21 Mar 2018
Thank You for your help. Now it works fine

Connectez-vous pour commenter.


narimen boucenna
narimen boucenna le 16 Juil 2020
is there any command i can use instead of
readtable
? i have matlab 2009 and i dont have this command in it
  1 commentaire
Image Analyst
Image Analyst le 16 Juil 2020
Wow, that is an antique. Try other functions like dlmread(), csvread(), importdata(), xlsread(), textscan(), etc.

Connectez-vous pour commenter.


Mahuya
Mahuya le 19 Nov 2022
To use readtable() function without any error you have to upload your file (from where you want to fetch data)into matlab drive/the folder you are using for matlab files. Then you can successfully able to fetch data from that file.
  1 commentaire
Image Analyst
Image Analyst le 19 Nov 2022
That's not a requirement. I never do that and I use readtable all the time. For example, I have files on my local drive (not on the MATLAB drive or any other cloud server) and readtable works just fine on those local files.

Connectez-vous pour commenter.


WEN-HAN
WEN-HAN le 4 Jan 2025
Error using readmatrix
Unable to find or open 'C:\Users\93060\OneDrive\桌面\dataset2\unknown\.'. Check the path and filename or
file permissions
  1 commentaire
Walter Roberson
Walter Roberson le 4 Jan 2025
You are trying to readmatrix() a filename that represents a directory. The name you are providing to readmatrix() is missing the final file name part, such as
'C:\Users\93060\OneDrive\桌面\dataset2\unknown\Book2.xlsx'

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by