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

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

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

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

Yes. I checked the path and I added addpath(<path to the directory located the sample.txt file). Even after adding the path in the script and add the location to the matlabpath, I checked exist('sample.txt','file') and its still returning zero (0). So I am thinking why.
The original file is an output of FORTRAN code and I opened in notepad ++. The oroginal file (OUTPUT) did not show the extension .TXT so I renamed as .TXT. But the oroginal FORTRAN output file opens in Notepad ++ or notepad.
Any help is highy appreciated to open this file in MATLAB.
Thanks again.
I am having a similar issue. When I try to load the file via a script I get the "unable to open" file error, but if I use the command line and copy and paste the exact same file, it opens fine. I'm sure I'm missing something small, but any help is welcome. I do have the entire path pointing to the file. (I am using a list of files and reading each one through a loop)
i.e
list_dat_files =
TC_sample.lst
>> exist(list_dat_files)
ans =
2
>> fid=fopen(list_dat_files,'r')
A = textscan(fid,'%s');
B = A{1};
C = char(B);
fclose(fid);
fid =
3
>> C(1,:)
ans =
/data/kenley_j/sample_file.dat
>> exist(C(1,:))
ans =
0
>> other = '/data/kenley_j/sample_file.dat'
other =
/data/kenley_j/sample_file.dat
>> exist(other)
ans =
2
Hmmm...I can't see this issue at first blush, but...
Use the alternate error message return from fopen and see what it tells you that may be informative in trying to open it from within the script
[fid,msg]=fopen(...
Also echo
cd
matlabpath
from the script and see where it's operating at what the in-context search path is. It's got to be something along that line...
Unfortunately, I never saw the previous poster's return comment; probably because back last spring about that time was away for a couple of weeks. Hopefully he's opened his file by now rather than still waiting... :)
Thanks! I'll give these a shot and let you know!
And it would be hysterical if they were still waiting! : )
OBTW, it would be a_good_thing (tm) to include the 'file' optional argument to exist as a performance-enhancing device. Otherwise, exist will work thru all the myriad possibilities and it also ensures you're looking for what you want, specifically, in this usage.
First, thanks for the last tip. Good to know.
Second, I figured out why things were loading. I am cycling through a text file that contains files that have different character lengths. When I took out the "odd files" that were ~3-6 characters shorter, the code ran. The only issue that I need those files in there. Is there a way to get Matlab to "ignore" this?
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
HAZZAH! Not using char helped! Thanks!

Sign in to comment.

More Answers (3)

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'
>>
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)
a3=importdata('co2a0000369.rd.000');
Error using importdata
Unable to open file.
Error in Data_set (line 3)
a3=importdata('co2a0000369.rd.000');

Categories

Asked:

on 22 May 2014

Answered:

on 23 Jul 2022

Community Treasure Hunt

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

Start Hunting!