Cell array for legend in a plot loop

조회 수: 15 (최근 30일)
Romain Liechti
Romain Liechti 2019년 6월 7일
댓글: Romain Liechti 2019년 6월 7일
I've built a function to plot a structure of idfrd, that use the fieldnames of the structure as legend for the curves. Everything works fine so far but I got a warning when I'm using the cell array for legend in the loop. 'Warning: Ignoring extra legend entries.' How can I get rid of this?
% Get field names as a cell array
names = fieldnames(Struct);
% Declare the cell array for legend
LegendCell = cell(length(names),1);
% Create the cell array containing the name of the fields for the legend
for jj = 1:length(names)
LegendCell{jj} = names{jj};
LegendCell{jj} = strrep(LegendCell{jj},'_',' '); % replace underscore by space
end
for ii = 1:length(fieldnames(Struct))
[mag,pha,~] = bode(Struct.(names{ii}),f_ax*2*pi);
figure(1)
subplot(211)
semilogx(f_ax,20*log10(squeeze(mag)),'linewidth',2)
hold on
l = legend(LegendCell{ii},'Location','southwest');
set(l,'FontSize',12,'FontName','Helvetica');
end

채택된 답변

Alex Mcaulley
Alex Mcaulley 2019년 6월 7일
Putting the legend call out of the loop:
% Get field names as a cell array
names = fieldnames(Struct);
% Declare the cell array for legend
LegendCell = cell(length(names),1);
% Create the cell array containing the name of the fields for the legend
for jj = 1:length(names)
LegendCell{jj} = names{jj};
LegendCell{jj} = strrep(LegendCell{jj},'_',' '); % replace underscore by space
end
for ii = 1:length(fieldnames(Struct))
[mag,pha,~] = bode(Struct.(names{ii}),f_ax*2*pi);
figure(1)
subplot(211)
semilogx(f_ax,20*log10(squeeze(mag)),'linewidth',2)
hold on
end
l = legend(LegendCell,'Location','southwest');
set(l,'FontSize',12,'FontName','Helvetica');

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by