Load and do operations for multiple files in a loop

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

 Accepted Answer

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 Comments

Hi Ameer,
Thank you so much. I get an error on line 6:
Unrecognized table variable name 'x'.
I have had this error before too. Not sure why it has difficulty accessing this column of the table?
Any workarond?
Much appreciated,
Pasha
Pasha
Pasha on 23 Jun 2020
Edited: darova on 24 Jun 2020
So 'data' is a table which hold 'ATA*' as a structure. Pictures below.
You can directly load the variable into the workspace instead of loading them into a variable. For this solution, replace the line
data = load(theFiles(k).name);
with
load(theFiles(k).name);
However, this is not a recommended practice but will make the solution easy in this case.
An alternative method can be like this
data = load(theFiles(k).name);
data = struct2table(data.(theFiles(k).name));
Thank you Amir.
filePattern = fullfile('ATA*.mat'); %the 306 files are in the following format ATA 1....306.mat
theFiles = dir(filePattern);
for k= 1:numel(theFiles)
load(theFiles(k).name);
data = struct2table(theFiles(k));
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
I rran the above code. But, there is a problem with line 5. It generates the following table which is not correct.
It runs fine if I input the file name manually e.g.
data = struct2table(ATA001);
Can you share a sample .mat file with this data?
Hi Ameer,
Sure! have now attached.
Regards,
Pasha
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
That works great! Thank you very much

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Asked:

on 23 Jun 2020

Commented:

on 25 Jun 2020

Community Treasure Hunt

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

Start Hunting!