insert and run multiple files

12 views (last 30 days)
laser laser12
laser laser12 on 13 Jul 2015
Answered: Image Analyst on 14 Jul 2015
Dears, I tried to import multiple (.dat) files. It was not possible to read them by using (importdata or dlmread). always appear an error.where is the problem?
files = dir('*.dat');
for K = 1:length(files);
data(K,:) = dlmread(files(K).name, ';');
end ;
please, i need help.
Regards,
  3 Comments
laser laser12
laser laser12 on 13 Jul 2015
Error using dlmread (line 119) The file 'pl1.dat' could not be opened because: No such file or directory.
*The same message appears if I use importdata
dpb
dpb on 13 Jul 2015
Show us the script in context; it would seem that if length(files)>0 then there should be at least one file opened from the above. Ergo, one is left to conclude somehow something else is going on since the message is clear that the file doesn't actually exist.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 14 Jul 2015
I find that hard to believe. If dir() found it, then it won't subsequently say it's not there. Here, try this more robust code and tell us what it says:
folder = pwd; % Whatever folder you want.
filePattern = fullfile(pwd, '*.dat');
files = dir(filePattern);
if ~isempty(files)
numberOfFiles = length(files)
% Initiliaze data. Assume one row and 42 columns of data in each data file.
data = zeros(numberOfFiles, 42);
for K = 1:length(files)
thisFileName = fullfile(folder, files(K).name);
fprintf('Trying to open %s\n', thisFileName);
if exist(thisFileName, 'file')
% Read in row vector from file.
data(K,:) = dlmread(thisFileName, ';');
else
message = sprintf('Warning: %s does not exist', thisFileName);
uiwait(warndlg(message));
end
end
else
message = sprintf('Warning: no .dat files in folder\n%s', folder);
uiwait(warndlg(message));
end
Obviously, change 42 to however many columns there actually are in your data file.

Tags

Community Treasure Hunt

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

Start Hunting!