matlab loop code to save figures

1 view (last 30 days)
Benjamin on 7 Nov 2018
Commented: Jan on 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.
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 =;
%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]);
%dPdD = gradient(pressure(:,1)) / gradient(density);
[~,dPdD] = gradient(pressure, mean(diff(density)));
[~,ddPddD] = gradient(dPdD, mean(diff(density)));
plot(density, ddPddD)
grid on;
ylim([-0.2 0.5])
xlim([0.4 0.68])
xlabel('\rho (g/cm^3)');
  1 Comment
Jan on 8 Nov 2018
"It starts with dr_1e-1" - I see the variable fname starting with 'dr_1e-2'.

Sign in to comment.

Answers (1)

Jan on 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);
It is not clear what you have to change in the code.


Community Treasure Hunt

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

Start Hunting!