Is there a way to pass in N datasets and plot them in a line graph using for loop?
I did it by passing fixed number of parameter ( eg M1, M2, M3, M4), and repeat plotting manually like below. But I wonder if there are way to code the function dynamically? Let say I can pass in 4 datasets, or 40 datasets, and plot them in one graph via looping.
function plot_four_cdf(M1,M2,M3,M4)
[ycdf1,xcdf1] = cdfcalc(M1);
ycdf1 = ycdf1(2:length(ycdf1));
plot(xcdf1, ycdf1, '-+k', 'LineWidth', 1);
hold on;
[ycdf2,xcdf2] = cdfcalc(M2);
ycdf2 = ycdf2(2:length(ycdf2));
plot(xcdf2, ycdf2, '-ok', 'LineWidth', 1);
hold off;
hold on;
[ycdf3,xcdf3] = cdfcalc(M3);
ycdf3 = ycdf3(2:length(ycdf3));
plot(xcdf3, ycdf3, '-*k', 'LineWidth', 1);
hold off;
hold on;
[ycdf4,xcdf4] = cdfcalc(M4);
ycdf4 = ycdf4(2:length(ycdf4));
plot(xcdf4, ycdf4, '-sk', 'LineWidth', 1);
legend('M100','M80','M50','M20',...
'Location','SE')
xlabel('Relative Error');
ylabel('CDF');
end
You can group all your data in a cell-array and pass that to the function:
Note that
Mis constructed something likepossibly also in a loop of its own. Note also that the
legendentriesare now kind of hard to define. You can pass them as a separate argument to the function (better option), or stuff them in the same cell arrayMnext to the data they describe (not very portable).Note also that you’d have to do some error checking (now, only 8 different plots can be made. An error will result if you do more).