I am getting an 'Unable to open file.' error with importdata?
210 次查看(过去 30 天)
显示 更早的评论
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 个评论
Lionel Jayasinghe
2020-8-14
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 ,
采纳的回答
dpb
2014-5-22
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 个评论
dpb
2014-11-7
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
更多回答(3 个)
dpb
2014-5-22
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.
2021-6-17
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)
0 个评论
Shveta
2022-7-23
a3=importdata('co2a0000369.rd.000');
Error using importdata
Unable to open file.
Error in Data_set (line 3)
a3=importdata('co2a0000369.rd.000');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!