Loop to plot PDF over bar plot
Mostrar comentarios más antiguos
I am trying to write code to plot a line from a probability density function over the bar plot from which it's derived. The data for the bar plot is stored in an matrix called gsd. The first column in the scale and columns 3:19 are data. The PDF data is stored in a matrix called deco. The labels are stored in a 1x19 cell called label. Each PDF has a different scale. When I plot them one at a time I do as follows:
bar(gsd(:,1), gsd(:,3))
hold on
plot(deco(:,1), deco(:,2), 'LineWidth', 3)
xlabel ('Grain size (phi)')
ylabel ('Weight Percent')
title (label(:,3))
For each sample I change the y of the bar plot to be the next column, the x and y of the line to be the next two columns, and the title to the next column over. I'd like to do this as a loop, but I'm having trouble coding the loop to loop through 3 different matrices.
I've managed to loop making the bar plot but I can't figure out how to save the file or make the title each columns' label. I also split the deco matrix into just the scale and pdf because I couldn't figure out how to make it call two at a time from one matrix. This cycles through but it never stops and the plotted lines become increasing nonesensical. Below is my code. I'm sure some of the naming is non-standard. I'm self taught so please tell me everything I'm doing wrong so I can learn. Thanks!
for ColIdx= 3:19 % grain size data
for i= 3:19 % gsd labels
for j = 1:17 % pdf scale
for k= 1:17 % pdf
xdata= gsd(:,ColIdx); % tells loop what each loop is pulling from
qlabel= label(:, i);
qscale= scale(:,j)
qpdf= pdf(:,k)
filename = sprintf('i_GSD.png', ColIdx);
figure;
bar(gsd(:,1), xdata)
xlabel ('Grain size (phi)');
ylabel ('Weight Percent (%)');
title (sprintf ('i_GSD', ColIdx));
hold on
plot( qscale, qpdf, 'LineWidth', 3)
% Save the plot as a PNG file
saveas(gcf, filename);
close(figure)
end
end
end
end
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!