MATLAB: Read in files with loop

data importloopMATLAB

I have the code below which reads in the data to log200. The thing is, I also need to read in other data too.
I need log200, log225, log250, in increments of 25 up to log900
How can I take my current working script which reads in log200, and replace it with a loop so that I do not have to copy and paste dozens of times?
clear
clc
lastn=1;
log200=importdata('C:/Users/Ben/Desktop/log.200_equil', ' ', 20, 0);
log200_cell = log200.data(:,6);
log200_cell = log200_cell(1:end-lastn,:);
log200_density_average=mean(log200_cell)

Best Answer

Simpler to use indexing and fullfile:
D = 'C:/Users/Ben/Desktop/';
S = dir(fullfile(D,'*_equil')); % where is the file extension?
N = numel(S);
C = cell(1,N);
M = nan(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
fprintf('Now reading %s\n',F);
T = importdata(F,' ',20);
C{k} = T.data;
M(k) = mean(T.data(1:end-1,6));
end
All data will be in cell array C, and M should be all of the mean data joined together into one numeric vector. You will need to check the options for importdata: I don't have this so I can't run this code!
"... then take the average of the last 100 points in the 6th column"
If you really want the last 100 points, then perhaps you will need this:
D{k} = mean(T.data(end-100:end-1,6));