Regular expression while import data

2 views (last 30 days)
Bikram Kawan
Bikram Kawan on 12 Jun 2015
Commented: Walter Roberson on 14 Jun 2015
I have a nearly 360 folders which individual folder contains 400 .mat files. There are basically two structure in file names. Like
20130103_083800_p001_calc.mat
20130103_083800_p001.mat
20130103_083800_p002_calc.mat
20130103_083800_p001.mat
I have created script to load and do some calculations ( mean ) of every column of data.This script works for all files. Now I only want to load files without "calc" in their file name.
numfiles = 10;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('20130103_083800_p00%d.mat', k);
mydata{k} = importdata(myfilename);
fprintf('value of k: %d\n',k);
if k==1
a=mean(mydata{1,1}.module02.data,1)
a=[mydata{1,1}.startTime a];
end
temp1=mean(mydata{1,k}.module02.data,1)
temp1=[mydata{1,k}.startTime temp1];
a=[a;temp1];
end
Can any one suggest me. I will be really great full. I need to process many directories like this.

Answers (1)

Walter Roberson
Walter Roberson on 13 Jun 2015
That script will never try to importdata() from a file with "calc" in its name.
Anyhow, if you have a filename, perhaps found via dir(), then
if regexp(filename, '_calc')
%then there was a _calc so react appropriately such as with "continue"
end
  2 Comments
Bikram Kawan
Bikram Kawan on 13 Jun 2015
I have made some changes from above script like below;
matFiles = dir('*.mat');
numfiles = length(matFiles);
mydata = cell(1, numfiles);
temp2=0;
for k = 1:numfiles
mydata{k} = load(matFiles(k).name);
fprintf('value of k: %d\n',k);
if k==1
a=mean(mydata{1,1}.module02.data,1)
a=[mydata{1,1}.startTime a];
end
temp1=mean(mydata{1,k}.module02.data,1);
temp2=temp2+temp1;
temp1=[mydata{1,k}.startTime temp1];
a=[a;temp1];
end
This works for me to load all mat files and do some calculations.But now need to use regular expression like you said is it possible ?
Walter Roberson
Walter Roberson on 14 Jun 2015
Guessing a bit at what you want:
matFiles = dir('*.mat');
not_calc = cellfun(@isempty, regexp({matFiles.name}, '_calc'));
matFiles - matFiles(not_calc);
and then continue on with numfiles = and so on.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!