I am getting an 'Unable to open file.' error with importdata?
Show older comments
Hi,
I am trying to open a text (sample.txt) file in MATLAB using importdata function. But I am getting an error for the following code. Please see sample.txt file is attached. (it does not have a header and columns are separated by space)
A=importdata('sample.txt',' ',0);
Error using importdata (line 136) Unable to open file.
Can somebody suggest me why I am getting this error?
Thanks in advance.
1 Comment
Lionel Jayasinghe
on 14 Aug 2020
I am trying to open ex2.pdf file , to continue with machine learning exercise 2 . I cannot open the file Error using web>find_path (line 115)
Directory does not exist.
Error in web (line 89)
html_file = find_path(html_file);
Also it did not asked for any token ,
Accepted Answer
More Answers (3)
dpb
on 22 May 2014
If the file name is correct and not misspelled and in the location you think it is, there's no reason Matlab can't open it, regardless of the source. That is was written by Fortran is of no bearing. That you opened it in Notepad probably means you navigated to the proper location in the file open dialog so you removed any path dependency by manual navigation.
For Matlab,
1) Check what the actual path is by executing path to ensure you got the right subdirectory added w/o a typo or somesuch.
2) Do a manual dir on the file root name w/ a wildcard in the known directory...something like
dir c:\yourfilesdirectory\*samp*.*
to confirm.
I just made a quick test here to confirm Matlab works as described and I wasn't making stuff up... :)
Before doing anything...
>> path
MATLABPATH
c:\ML_R2012b\work
C:\ML_R2012b\toolbox\matlab\demos
C:\ML_R2012b\toolbox\matlab\graph2d
...
>> dir c*.txt % look for some .txt files in cwd...
ContainerData.txt cttn.txt cycle.txt
>> dir c:\temp\*.txt % and in a directory not on path...
CHRIS.TXT TESTCASE.TXT rand.txt ...
>> exist('chris.txt','file') % try to find one in \temp ...
ans =
0
% couldn't find it; now try *addpath*
>> addpath('c:\temp') % add the \temp directory
>> type chris.txt % see what's in the file over there
ss45gg67tt
ss gg67 t
>> exist('chris.txt','file') % check that can find it
ans =
2
>> dir c*.txt % observe it's _not_ in cwd...
ContainerData.txt cttn.txt cycle.txt
>> cd % cwd is still my default location
c:\ML_R2012b\work
>>
The upshot is, you've gotten something wrong but I can't see your system from here so can't unequivocally tell you what it was/is; all I can tell you is that if you get the path right or the fully-qualified name right, Matlab will open it just fine.
One last alternative...
>> fn=fullfile('c:\temp','chris.txt') % put the name in a variable
fn =
c:\temp\chris.txt
>> importdata(fn) % read it explicitly-defined fully-qualified name
ans =
'ss45gg67tt'
'ss gg67 t'
>>
Thomas D.
on 17 Jun 2021
0 votes
I stumbled across this having the same problem (error message "Unable to open file.", but 100% sure it was the correct path), but I found another solution.
After I opened the file with Notepad++, added and deleted some random character and saved the file, everything worked fine. Probably this is kind of a weird Windows issue?!
By the way, fopen worked for my file even before the Notepad stuff, but exist() gave me a FALSE. Really strange...
(I was using Windows 7 and Matlab R2015a)
Shveta
on 23 Jul 2022
0 votes
a3=importdata('co2a0000369.rd.000');
Error using importdata
Unable to open file.
Error in Data_set (line 3)
a3=importdata('co2a0000369.rd.000');
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!