insertShape
は、画像のRGBデータを直接変更し、挿入される図形のグラフィックスオブジェクトを生成しません。彼らは、1つの回避策として、the valid object types for legend
を、あなたはあなたの図形の各ダミーラインシリーズを生成することができNaN
値はMATLABで視覚的にレンダリングされませんので:。このため、何のグラフィックスは(も参照表示するlegend
のハンドルはありませんあなたの軸に表示されていないラインオブジェクトを作成するために使用することができる。例えば
:。
:
% Read sample image
RGB = imread('peppers.png');
imshow(RGB);
% Add some annotations
dim1 = [0.3 0.7 0.2 0.2];
annotation('rectangle', dim1, 'Color', 'red')
dim2 = [0.6 0.5 0.1 0.1];
annotation('rectangle', dim2, 'Color', 'blue')
x1 = [0.5 0.6];
y1 = [0.7 0.5];
annotation('line', x1, y1, 'Color', 'green', 'LineWidth', 2)
x2 = [0.5 0.7];
y2 = [0.9 0.6];
annotation('line', x2, y2, 'Color', 'magenta', 'LineWidth', 2)
% ============== EVERYTHING ABOVE THIS LINE IS SPECIFIC TO MY EXAMPLE, IGNORE IT
% Plot dummy lineseries for legend
% Specify 'DisplayName' for legend, can also be done manually in legend call
hold on
tmp(1) = plot(NaN, 'Color', 'green', 'LineWidth', 2, 'DisplayName', 'Green Line');
tmp(2) = plot(NaN, 'Color', 'magenta', 'LineWidth', 2, 'DisplayName', 'Magenta Line');
hold off
% Display legend
legend(tmp)
次生成
'legend'は' plot'、 'scatter'などとしか動作しないと思います。[' annotation']を使って必要な場所に文字列を書き込もうとします(http:// www。 mathworks.com/help/matlab/ref/annotation.html)を「凡例」の代わりに使用してください! –
つまり、 'legend'には' axes'オブジェクトが必要です。 – EBH