insert and run multiple files

12 Ansichten (letzte 30 Tage)
laser laser12
laser laser12 am 13 Jul. 2015
Beantwortet: Image Analyst am 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 Kommentare
laser laser12
laser laser12 am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 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!

Translated by