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

4 Ansichten (letzte 30 Tage)
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.
  2 Kommentare
Stephen23
Stephen23 am 13 Mär. 2018
"The file names are not sequentially numbered"
What does this mean exactly? Please show some examples of the filenames. Also please tell us what order you want them processed in.
SGMukherjee
SGMukherjee am 13 Mär. 2018
Bearbeitet: Stephen23 am 13 Mär. 2018
This code runs on input text files with structured numerical data to generate a figure with the results. How can I run the program on multiple input files, say
Cyprus.2010.051.09.47.G31.txt
Cyprus.2010.225.20.58.G09.txt
etc
all at once and get a plot for each input?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 13 Mär. 2018
Bearbeitet: Rik am 13 Mär. 2018
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
  3 Kommentare
SGMukherjee
SGMukherjee am 13 Mär. 2018
Thank you so much Mr. Wisselink. Could you please check the screenshot of the error message? Is there any way I could title my figure depending on the input file used?
Rik
Rik am 13 Mär. 2018
Have you read the documentation for textscan? It should be immediately apparent what mistake I made. As the error message tells you, textscan doesn't support 3 outputs, because it stores the result in a cell.
part=textscan(filename_edit,'Cyprus;%d;%d;%s');
saveas(gcf,sprintf('%03d_%04d.jpg',part{2},part{1}))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by