Regular expression while import data

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.

回答(1 个)

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 个评论

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 ?
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by