matlab loop code to save figures
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Jan
2018-11-8
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!