MATLAB: Change the structure of multiple .txt files and store them as .mat files

multiple files

I have 15 .txt files that I choose them from my directory and I need to change their structure and save them as .mat files. So far I can only do this for 1 file.
Here is my code:
d=dir;
dstr={d.name};
[s,ok]=listdlg('PromptString','Select Catalog:',...
'SelectionMode','Double',...
'ListString',dstr);
fid=fopen(dstr{s});
a=fscanf(fid,'%4d %2d %2d %2d %2d %g %g %g %g %g \n',[10,inf]);
a=a';
fclose(fid);
%--------------------------------------------------------------
IDQ=a;
% time
year = IDQ(:,1);
month = IDQ(:,2);
day = IDQ(:,3);
hour = IDQ(:,4);
minute = IDQ(:,5);
second = IDQ(:,6);
timeVec = [year,month,day,hour,minute,second];
time = datenum(timeVec);
catalog.time = time;
save('catalog.mat','catalog');

Best Answer

txtfiles = dir('*.txt') ; % you ar eint he folder of text files
N = lenght(txtfiles) ; % Total number of files
for i = 1:N
thisfile = txtfiles(i).name ;
% read file
fid=fopen(thisfile);
a=fscanf(fid,'%4d %2d %2d %2d %2d %g %g %g %g %g \n',[10,inf]);
a=a';
fclose(fid);
%--------------------------------------------------------------
% IDQ=a;
% % time
% year = IDQ(:,1);
month = IDQ(:,2);
day = IDQ(:,3);
hour = IDQ(:,4);
minute = IDQ(:,5);
second = IDQ(:,6);
timeVec = [year,month,day,hour,minute,second];
time = datenum(timeVec);
catalog.time = time;
filename = [catalog,num2str(i),'.mat'] ;
save(filename,'catalog');
end
end