Importing multiple .dat files in MATLAB

13 ビュー (過去 30 日間)
Anirudh Mehta
Anirudh Mehta 2018 年 1 月 11 日
編集済み: per isakson 2018 年 1 月 12 日
I have multiple(100-200) .dat files which contain data,when I open them in matlab it opens in import tab. I want to write a generic program so that all data can be accessed from 1 script. I mean all data should be accessible from work-space, and each .dat file could be represented in a cell array.
I have tried to generate a function , from default import function but I cannot do it for 100 files. Uploading 5 dat files for example, if someone could help me write a generic code, for these 5 files, and can be extended to 100 files. Thanks in advance
  4 件のコメント
per isakson
per isakson 2018 年 1 月 11 日
Don't you need the start time, e.g.
Start Data: 9/12/2016 7:46:30 PM 552 ms
Anirudh Mehta
Anirudh Mehta 2018 年 1 月 11 日
If it is there its good, otherwise it is fine.

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

回答 (2 件)

KSSV
KSSV 2018 年 1 月 11 日
編集済み: KSSV 2018 年 1 月 11 日
datfiles = dir('*.dat') ; % get all dat files in the folder
N = length(Datfiles) ; % total number of files
for i = 1:N % loop for each file
thisfile = datfiles(i).name
% do what you want
end
  2 件のコメント
Anirudh Mehta
Anirudh Mehta 2018 年 1 月 11 日
It does not import data..! I want to import 1 .dat file as a cell array..! Thanks for your help.!
KSSV
KSSV 2018 年 1 月 11 日
The above code....give the names of all .dat files.....you need to use a function to load....I have shown only the part of accessing files.

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


per isakson
per isakson 2018 年 1 月 11 日
編集済み: per isakson 2018 年 1 月 12 日
Try
>> out = cssm( 'h:\m\cssm\trial*.dat' )
it should return
out =
1x5 struct array with fields:
name
time
colhead
data
where
function out = cssm( glob )
sas = reshape( dir( glob ), 1,[] );
folder = fileparts( glob );
out = struct( 'name', {}, 'time',{}, 'colhead',{}, 'data',{} );
for s = sas % loop over all files
dat.name = s.name;
str = fileread( fullfile( folder, s.name ) );
cac = regexp( str, '(?m)^Start Data:(.+)$', 'tokens', 'once' );
dat.time = strtrim( cac{:} );
cac = textscan( str, '%[^\n]', 1, 'Headerlines',8, 'Delimiter','\n' );
dat.colhead = strsplit( cac{:}{:} );
cac = textscan( str, repmat('%f', size(dat.colhead)) ...
, 'Headerlines',9, 'Collectoutput',true, 'Delimiter','\t' );
dat.data = cac{1};
out = cat( 2, out, dat );
end
end
  2 件のコメント
Anirudh Mehta
Anirudh Mehta 2018 年 1 月 11 日
編集済み: per isakson 2018 年 1 月 11 日
did not get
out = cssm( 'h:\m\cssm\trial*.dat' )
out =
1x5 struct array with fields:
name
time
colhead
data
this part.?
per isakson
per isakson 2018 年 1 月 11 日
編集済み: per isakson 2018 年 1 月 11 日
Do you know how to create and use a Matlab function?
Do you know Matlab stuctures?

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

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by