フィルターのクリア

How do I control the legend contents when the plot functions are within a for loop?

9 ビュー (過去 30 日間)
Brad
Brad 2014 年 6 月 12 日
編集済み: Star Strider 2014 年 6 月 12 日
I'm running into a problem pertaining to legend control in some legacy code. Specifically, how do I suppress the data2 and data4 entries of the legend (in the attached png file) using the following code:
figure('Name', 'Elapsed Time', 'Position', [350 350 610 250] );
hold on;
for FileNum = 1:num_srcs
h1 = plot(data{FileNum}(:, 9), '-*', 'Color', color1{FileNum}, 'DisplayName', disp_name{FileNum} );
%hold on;
h2 = plot( data2{FileNum}(:, 9), '*', 'Color', color1{FileNum} );
%hold off;
end
%hold off;
legend('location', 'NEO'); % works, but includes the legend entries for data2 and data4
%legend(h1);
%legend(h1, 'location', 'NEO');
%legend(h1, disp_name{FileNum}, 'location', 'NEO');
set(legend, 'FontSize', 20);
H = gca;
set(H, 'YGrid', 'on', 'GridLineStyle', '-');
box on;
The data2 and data4 data series are the points with no lines (far left side of plot).
In reading the MATLAB documentation and reviewing Aravin's question in January 2012, it appeared the solution was to include only h1 in the legend, using the following line of code;
legend(h1, 'location', 'NEO');
This did not produce the desired result. I've also tried re-locating the hold functions and other legend functions - none have worked.
Is it possible the for loop is adversely affecting the legend function?
Here's the link to Aravin's original question in January 2012: http://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure

採用された回答

Mischa Kim
Mischa Kim 2014 年 6 月 12 日
編集済み: Mischa Kim 2014 年 6 月 12 日
Brad, see this answer. Or, more closely related to your question:
x = 0:0.1:10;
data1 = sin(x);
data2 = cos(x);
data3 = sin(x).^2;
data4 = cos(x).^2;
figure();
p1 = plot(x,data1,'bo-');
hold on
p2 = plot(x,data2,'rx-');
p3 = plot(x,data3,'kd-');
p4 = plot(x,data4,'gs-');
legend([p1,p3],'From data1','From data3');
  4 件のコメント
Brad
Brad 2014 年 6 月 12 日
Would the workspace variables be sufficient?
Brad
Brad 2014 年 6 月 12 日
編集済み: Star Strider 2014 年 6 月 12 日
Mischa, it appears we found a solution. We added this line of code:
set(get(get(h2, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
after this line of code:
h2 = plot( data2{FileNum}(:, 9), '*', 'Color', color1{FileNum} );
So far, it appears to be running great.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by