I want my function to ignore some inputs
14 views (last 30 days)
Show older comments
Hello, I made a function called plotgeral that makes plots what I want with the format that I desire.
the 1st line of the function where i declare my outputs and inputs is:
out = plotgeral(Y,Y1,Y2,Y3,Y4,Y5,Xaxis title,Yaxis title,title,legend,legend1,legend2,legend3,legend4,legend5)
Sometimes I want to draw the plot with 6 inputs of data (Y -> Y5), but sometimes I just want the Y and Y1, for example. What should I do in this case? The name of Axis, title and legend are defined by me in the script with ' '.
I tried
out = plotgeral(Y,Y1,[],[],[],[],Xaxis title,Yaxis title,title,legend,legend,[],[],[],[])
but I get an error on the part of the legend, because [] it an invalid argument for the legend.
Thanks for your time.
2 Comments
Dennis
on 2 May 2018
Edited: Dennis
on 2 May 2018
you can replace legend.... with varargin, this way your function can accept more or fewer input arguments.
But i want to suggest to pass all data as 1 variable or structure instead of naming your variables/legends Y1,Y2,Y3; legend1,legend2. You could add another dimension to Y (Y(:,1), Y(:,2)) and check in your function the size of Y in that dimension (numberofplots=size(Y,2)) and do the same with the legend.
If you are interested in more information why dynamically naming your variables is usually a bad idea i recommend having a look at this post:
function plotgeral(Y,eixoX,eixoY,titulo,legenda)
C = {'k','b','c','g','m','r'};
original = figure();
set(original,'PaperUnits','centimeters','PaperPosition',[0 0 29 21],'PaperOrientation','landscape');
numberofplots=size(Y,2);
hold on
for ii=1:numberofplots
plot(Y(:,ii),'Color',C{ii},'Markersize',12);
end
set(gca,'FontSize',10);
grid on
title(titulo,'FontSize',12,'Color','black','FontWeight','bold');
xlabel(eixoX);
ylabel(eixoY);
legend(legenda)
ax = gca; % Eixos
ax.XAxis.LineWidth = 1.5;
ax.YAxis.LineWidth = 1.5;
ax.GridLineStyle = '-';
end
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!