matlab loop code to save figures

1 Ansicht (letzte 30 Tage)
Benjamin
Benjamin am 7 Nov. 2018
Kommentiert: Jan am 8 Nov. 2018
I have the code below. It works fine. Notice the filename. It starts with dr_1e-1. I have other files that go up to dr_1e-9. Is there a way to make my code loop through and create all of my figures or do I have to manually change it? Note that the title would need to change too.
fname='dr_1e-2_epax_1e-11_A1_OFF_A2_OFF.txt';
name = 'dr = 1e-7 - epax[0] = 1e-10 - A1 = OFF - A2 = OFF';
delimiterIn = ' ';
headerlinesIn = 5;
% Import the data file for post-processing
matrix = importdata(fname,delimiterIn,headerlinesIn);
A = matrix.data;
%Define the number of distinct isotherms
temp_ids = sum(A(:) == 0.2);
%Define the number of density points sampled
density_ids = length(A)/temp_ids;
%Grab density matrix
density = A((1:density_ids),1);
%Grab temperature matrix
pressure = A(:,3);
%Reshape temperature matrix so it has one column for each temperature
%(isotherm), rather than just a single long column
pressure = reshape(pressure,[density_ids,temp_ids]);
%Differentiate
%dPdD = gradient(pressure(:,1)) / gradient(density);
[~,dPdD] = gradient(pressure, mean(diff(density)));
[~,ddPddD] = gradient(dPdD, mean(diff(density)));
figure(1)
plot(density, ddPddD)
grid on;
ylim([-0.2 0.5])
xlim([0.4 0.68])
xlabel('\rho (g/cm^3)');
ylabel('\partial^2p/\partial\rho^2')
suptitle(name)
saveas(gcf,name)
  1 Kommentar
Jan
Jan am 8 Nov. 2018
"It starts with dr_1e-1" - I see the variable fname starting with 'dr_1e-2'.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 8 Nov. 2018
I guess all you need is
for k = 1:9
fname = sprintf('dr_1e%d-2_epax_1e-11_A1_OFF_A2_OFF.txt', k);
...
end
It is not clear what you have to change in the code.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by