insert and run multiple files

11 vues (au cours des 30 derniers jours)
laser laser12
laser laser12 le 13 Juil 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 commentaires
laser laser12
laser laser12 le 13 Juil 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 le 13 Juil 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.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 14 Juil 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