Regular expression while import data
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
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.
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!