Load and do operations for multiple files in a loop

2 ビュー (過去 30 日間)
Pasha
Pasha 2020 年 6 月 23 日
コメント済み: Pasha 2020 年 6 月 25 日
Hi everyone,
I have 306 files (1x1 struct with 2 fields: x and y) in the following format: ATA 1....306.mat. These represent coordinates from a digitized signal.
I want to read the files in MATLAB and do similar operations on all of them. The code I want to write in a loop is:
%convertstructure to table
data = struct2table(data)
%convert table to timetable
data.x= seconds (data.x)
data = table2timetable(data, 'RowTimes', x)
% resolve potentially duplicate times in preparation for next step
data= unique(data,'sort','rows')
%resample data (using nearest neighbour) in timetable to resolve issue of irregular times. d predefines a time step.
d= seconds(0.0001)
data = retime(data,'regular','nearest','TimeStep',d)
save ('data')
I have made a few different unsuccesful attempts. One of them being:
filePattern = fullfile('ATA*.mat'); %the 306 files are in the following format ATA 1....306.mat
theFiles = dir(filePattern);
for k= 1:306(theFiles);
data = load(theFiles.name);
data = struct2table(data);
data.x= seconds (data.x);
data = table2timetable(data, 'RowTimes', x);
data= unique(data,'sort','rows');
d= seconds(0.0001);
data = retime(data,'regular','nearest','TimeStep',d);
% then save as original file name
end
Your help would be much appreciated!
Regards,
Pasha

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 23 日
Try something like this
filePattern = fullfile('ATA*.mat'); %the 306 files are in the following format ATA 1....306.mat
theFiles = dir(filePattern);
for k= 1:numel(theFiles)
data = load(theFiles(k).name);
data = struct2table(data);
data.x= seconds (data.x);
data = table2timetable(data, 'RowTimes', x);
data= unique(data,'sort','rows');
d= seconds(0.0001);
data = retime(data,'regular','nearest','TimeStep',d);
% then save as original file name
save(theFiles(k).name);
end
  8 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 6 月 25 日
Try this
filePattern = fullfile('ATA*.mat'); %the 306 files are in the following format ATA 1....306.mat
theFiles = dir(filePattern);
for k= 1:numel(theFiles)
data = load(theFiles(k).name);
data = struct2table(data.(theFiles(k).name(1:end-4)));
data.x= seconds (data.x);
data = table2timetable(data, 'RowTimes', x);
data= unique(data,'sort','rows');
d= seconds(0.0001);
data = retime(data,'regular','nearest','TimeStep',d);
% then save as original file name
save(theFiles(k).name);
end
Pasha
Pasha 2020 年 6 月 25 日
That works great! Thank you very much

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by