What is the code to plot several .dat files together?

I have 1000 .dat files and I want to plot graphs from them and save the results in .png format for all the files. How do we do that?

1 Kommentar

I have the same question.
The answer down here are not working for me.
@KSSV

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

KSSV
KSSV am 22 Mai 2017
figure
hold on
datfiles = dir('*.dat');
for K = 1 : length(datfiles)
data = load(datfiles(k).name); %load just this file
% Plot the data depending on variables
end

6 Kommentare

Thanks. But, I want to store all the plots in .png format and then make an animation of how the system is evolving with time.
I am working on Molecular dynamics simulations. So I have 10000 .dat files which shows the state of the system at different instants and I want to generate a plot of these files and make an animation out of them.
KSSV
KSSV am 22 Mai 2017
can you please help me how to sort all the files? My file names are t-0.dat, t-1.dat,t-2.dat,t-4.dat,...t-16.dat,...., t-128.dat,........t-10000.dat. But when I'm using the code to plot the results, it reads t-0, t-1,t-128,t-10000, then t-2,....t-4... like this. I want to get them in order. How do I do it? (I have modified your code a bit to get the plots).
datfiles1 = dir('*.dat');
for k = 1 : length(datfiles)
data = load(datfiles(k).name);
h=figure;
scatter(data(:,1),data(:,2),50);
hold on;
quiver(data(:,1),data(:,2),data(:,4),data(:,5),0.3);
end
saveas(h,sprintf('fig%d.png',k));
hold off;
end
KSSV
KSSV am 22 Mai 2017
You can try this file exchange function by Stephen Cobeldick:
Thanks. But, I have already tried using natsortfiles,it did not work. I could not apply it properly as I'm new into Matlab. Can you please help me regarding how I should apply natsortfile function here to sort the files?
datfiles = dir('*.dat');
for k = 1 : length(datfiles)
data = load(datfiles(k).name);
h=figure;
scatter(data(:,1),data(:,2),50);
hold on;
quiver(data(:,1),data(:,2),data(:,4),data(:,5),0.3);
end
saveas(h,sprintf('fig%d.png',k));
hold off;
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-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