MATLAB: How to create a mean velocity matrix from multiple .dat files and plot the mean profile

for loop import multiple files

I have a folder of x no of *.dat files. So far I've imported 1 file and reshaped the columns to get matrices for each column. e.g for 1 dat file:
filename='B00049.dat';
delimiterIn= ' ';
headerlinesIn = 3;
A= importdata(filename,delimiterIn,headerlinesIn);
%% Matrices%%%%
c = 214;%columns i
r = 134;%rows j
dt= 0.0005;
x=reshape(A.data(:,1),r,c);
y=reshape(A.data(:,2),r,c);
u=reshape(A.data(:,3),r,c);
v=reshape(A.data(:,4),r,c);
I now want to import multiple files and creat a mean velocity matrix fro u and v then plot a profile. How can this be done ? Thanks

Best Answer

files=dir('*.dat') %If files are in current dir, otherwise enter entire path
%%Preallocate some cells
x=cell(1,numel(files))
y=cell(1,numel(files))
u=cell(1,numel(files))
v=cell(1,numel(files))
%%Loop over all files
for i=1:numel(files)
filename=files(i).name
delimiterIn= ' ';
headerlinesIn = 3;
A= importdata(filename,delimiterIn,headerlinesIn);
c = 214;%columns i
r = 134;%rows j
dt= 0.0005;
%%Save results
x{i}=reshape(A.data(:,1),r,c);
y{i}=reshape(A.data(:,2),r,c);
u{i}=reshape(A.data(:,3),r,c);
v{i}=reshape(A.data(:,4),r,c);
end
When you have all the data stored in cells, you can concatenate along the third dimensinon and take the average. I assume all files share the same size?
V = cat(3, v{:});
mean(V,3)