필터 지우기
필터 지우기

I want my function to ignore some inputs

조회 수: 9 (최근 30일)
Tiago Dias
Tiago Dias 2018년 5월 2일
댓글: Tiago Dias 2018년 5월 3일
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
Dennis
Dennis 2018년 5월 2일
편집: Dennis 2018년 5월 2일
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
Tiago Dias
Tiago Dias 2018년 5월 3일
Thanks it works, had to change the colours to black yellow blue etc, the full name, just the letters for some reason gave me a problem.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by