MATLAB: How to process and plot the data from the .txt files when the filenames rae not sequential

data importplotting

filename = 'Cyprus.2010.101.07.52.G06.txt';
delimiterIn = ' ';
% headerlinesIn = 1;
A = importdata(filename,delimiterIn);
Altitude = A(:,1);
Digisonde = A(:,2);
COSMIC = A(:,3);
plot(Altitude, Digisonde,'--blo', Altitude, COSMIC, ':r*')
title('Day Number:101 Year:2010 07:52 UT Station: Cyprus')
xlabel('Altitude (Km)')
ylabel('Electron Density (/m3)')
legend('Digisonde', 'COSMIC')
saveas(gcf,'101_2010.jpg')
This code gives plot for a single .txt file. But if I want to process all the .txt files in the same directory in a loop, how to proceed? The file names are not sequentially numbered. Please help.

Best Answer

You can use dir to generate a file list and apply any sort order you wish.
list=dir('Cyprus*.txt');
Edit: the code below is more ready-to-run
list=dir('Cyprus*.txt');
for n_file=1:length(list)
filename=list(n).name;
filename_edit=strrep(filename,'.',';');%make textscan easier
[part1,part2,~]=textscan(filename_edit,'Cyprus;%d;%d;%s');
delimiterIn = ' ';
% headerlinesIn = 1;
A = importdata(filename,delimiterIn);
Altitude = A(:,1);
Digisonde = A(:,2);
COSMIC = A(:,3);
plot(Altitude, Digisonde,'--blo', Altitude, COSMIC, ':r*')
title('Day Number:101 Year:2010 07:52 UT Station: Cyprus')
%you probably want to change the title here
xlabel('Altitude (Km)')
ylabel('Electron Density (/m3)')
legend('Digisonde', 'COSMIC')
saveas(gcf,sprintf('%03d_%04d.jpg',part2,part1))
end