insert and run multiple files

12 ビュー (過去 30 日間)
laser laser12
laser laser12 2015 年 7 月 13 日
回答済み: Image Analyst 2015 年 7 月 14 日
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 件のコメント
laser laser12
laser laser12 2015 年 7 月 13 日
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 2015 年 7 月 13 日
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.

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2015 年 7 月 14 日
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.

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by