Error in downloading sequence of txt files

2 次查看(过去 30 天)
Hello, I have a sequence of txt files, similar to the one attached, in a folder called 'runfiles'. I am using the code below to these files one by one. However, I am getting an error message. I have tried the same code with other txt files and it worked. Could you please tell me how I can fix this?-Thanks,K.
>>>>>>>ERROR MESSAGE:
"Error using load Unable to read file AbR1997.txt: No such file or directory.
Error in TSaddmissrowsNonLEAP (line 6) ALL=load(filename) %load the file to work with it"
>>>>>>>>CODE USED:
matfiles = dir(fullfile('N:', 'My Documents', 'MATLAB','runfiles', '*.txt'))%go to the folder where you have the files
for n=1997:1999% @@@@@
filename = ['AbR', int2str(n), '.txt'] % @@@@@'sdfhbx' this is the text in the name of the filename that is followed by the sequencial number which is 'i'
ALL=load(filename) %load the file to work with it
end

采纳的回答

Geoff Hayes
Geoff Hayes 2014-9-5
编辑:Geoff Hayes 2014-9-5
Katerina - are you running the code from the directory where those files are located, or some other directory? Is the directory that contains the files in your MATLAB search path?
If a file is in a directory/folder that is in the MATLAB search path, then you will be able to open (or load) that file by simply providing the file name, as you are doing with
ALL=load(filename)
But if the directory/folder for this file is not in the MATLAB search path, then the code will not be able to find that file and you will see the
Error using load
Unable to read file 'AbR1997.txt': no such file or directory.
You can do a couple of things - either add the directory (that contains these files) to the MATLAB search path, or better, just specify where the file is located. If we assume that your directory is N:/My Documents/MATLAB/runfiles, then build the folder name as
foldername = fullfile('N:', 'My Documents', 'MATLAB','runfiles');
and use this when we build the file name
for n=1997:1999
% @@@@@'sdfhbx' this is the text in the name of the filename that is
% followed by the sequencial number which is 'i'
filename = fullfile(foldername,['AbR', int2str(n), '.txt']);
%load the file to work with it
ALL=load(filename)
end
Try the above and see what happens!
  2 个评论
Katerina F
Katerina F 2014-9-5
Thanks Geoff, to add the directory (that contains these files) to the MATLAB search path worked; the other solution did not work
Geoff Hayes
Geoff Hayes 2014-9-5
Katerina - both solutions should have worked. Were the files in the directory N:/My Documents/MATLAB/runfiles, or were they somewhere else? If the latter, then you would have to use that directory as foldername.

请先登录,再进行评论。

更多回答(1 个)

Gitesh Nandre
Gitesh Nandre 2014-9-5
It appears that those text files are not in your current directory. In your code, you neither switch to the directory where text files are stored nor you specify the full path for files while loading them. You can achieve it in following way:
%get directory name
directory = fullfile('N:', 'My Documents', 'MATLAB','runfiles');
%get text file info for text files starting with 'AbR'
matfiles = dir(fullfile(directory, 'AbR*.txt'));
for filenum = 1:numel(matfiles)
%load the text files in workspace
load([directory '\' matfiles(filenum).name]);
end

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by