I am getting an 'Unable to open file.' error with importdata?

515 views (last 30 days)
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
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 ,

Sign in to comment.

Accepted Answer

dpb
dpb on 22 May 2014
The file isn't actually named 'sample.txt' or, more likely, it's not in the current working directory or on the matlabpath searched by Matlab.
Try
exist('sample.txt','file')
It'll undoubtedly return FALSE (0).
Or,
dir samp*.*
and see what files beginning w/ the string 'samp' are found in current directory.
Either use a fully-qualified file name or change to the directory as current or add the location to the matlabpath
  8 Comments
dpb
dpb on 7 Nov 2014
Well, I'd suggest first building the file so that it isn't so...use cell strings instead of an array of character strings (which must be padded to concatenate vertically). Besides not having the extra padding to deal with, it's easier to use a cell string as it is returned with just the cell subscript rather than the awkward above construction where you have to remember to use the second ':'. "Use the curlies, Luke" to dereference the cell content to get the character string when it's needed (that is,
B = A{1};
and
C = char(B);
are identically the same thing; there's no reason not to simply use
fid=fopen(A{i},'r');
there's no need for the intermediary variable nor an explicit char here.
Now, on the generation side, if there is some constraint there that the file names must be fixed-length strings and you can't fix that portion of the problem, then
fid=fopen(strtrim(A{i}),'r');
is the magic elixir.
NB that to discover these things, try
help strfun
and look at the myriad of functions for handling strings in Matlab. Not knowing that "strfun" is the magic string there, one starts with
help
and looks at the available categories from which to choose depending on the problem area Soon you remember the most common areas and can dispense with the first. Also become familiar with lookfor

Sign in to comment.

More Answers (3)

dpb
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.
Thomas D. on 17 Jun 2021
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
Shveta on 23 Jul 2022
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 Data Import and Analysis 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!