load multiple files with a condition on the file name

3 views (last 30 days)
Hi everyone!
Im kind of new to matlab and I have the following problem: I have many files as .mat files with the following names, all in one folder. So what I want to do is to load all BASF files first and save them into a new BASF folder and then load all BIOG files and save them into a new BIOG folder.
data20091019_1BASF.DE
data20091020_1BASF.DE
data20091021_1BASF.DE
data20091020_1BIOG.DE
data20091022_1BIOG.DE
data20091101_1BIOG.DE
So far, this is what I have tried :
clear all
dirData=dir('*.mat');
filenames={dirData.name};
stringi=filenames;
for k=1:length(dirData)
stringi=filenames;
basf=regexp(stringi,'^dataOut(\d+)_1BASF.DE_rek.mat$','match');
basf(cellfun(@(basf) isempty(basf),basf))=[]; %get rid of empty cell arrays
biog=regexp(stringi,'^dataOut_(\d+_1BIOG_p.DE_rek.mat$','match');
biog(cellfun(@(biog) isempty(biog),biog))=[];
load(basf);
end
The problem is when I try to load all the files included in the basf cell array it says " Argument must contain a string". So I dont know how to make matlab load all the files that contain BASF or BIOG in ther file names. Could you please help me? Thank you so much! Lourdes

Accepted Answer

Jos (10584)
Jos (10584) on 26 Mar 2011
dfBASF = dir('*BASF.mat') ;
for F = {dfBASF.name}
curF = F{:} ; % current file
disp(curF)
movefile (curF, fullfile('BASF',curF) ) ; % test this first!!
end
df = dir('*.BIOG.mat') ;
% ... etc ...
  2 Comments
Lu
Lu on 26 Mar 2011
Thank you Jos for your answer!
I tried it but it doesnt read the BASF files using the command '*1BASF.DE_rek', thats why I tried using regexp. Regexp can actually see which file names are BASF and which ones are BIOG but then I cant load the files.
Jos (10584)
Jos (10584) on 27 Mar 2011
You could (and should) modify the input to DIR to get your requirements! Something like '*BASF.DE.mat' might do ...

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations 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!