フィルターのクリア

Multiple legends with a variable in a figure legend

5 ビュー (過去 30 日間)
Raj Raj
Raj Raj 2014 年 10 月 3 日
コメント済み: Raj Raj 2014 年 10 月 5 日
Hi all,
I am trying to plot a figure with 3 curves. I need legends to the figures with varibales as legends. Here is my try. Please correct it.
figure(),
plot(x1, y1,'o', x2, y2, 'x' , coeffs(1)+x2, coeffs(2)+y2,'+')
legend('x1-y1; i-10:i','x2-y2; i+1:i+11', 'interpolated');
The above figure works fine. But need to add "i" value from the program. Hence I tried methods as follows
plot(x1, y1,'o', 'DisplayName', sprintf('x1-y1; i-10:i; i=%d', i), ...
x2, y2,'x', 'DisplayName', sprintf('x2-y2; i+1:i+11; i=%d', i), ...
coeffs(1)+x2,coeffs(2)+y2,'+','DisplayName',sprintf('Interpolated; i=%d', i));
The error is
Error using plot String argument is an unknown option.
Thanks for your attention...

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 10 月 3 日
Raj - the code
plot(x1, y1,'o', 'DisplayName', sprintf('x1-y1; i-10:i; i=%d', i), ...
x2, y2,'x', 'DisplayName', sprintf('x2-y2; i+1:i+11; i=%d', i), ...
coeffs(1)+x2,coeffs(2)+y2,'+','DisplayName',sprintf('Interpolated; i=%d', i));
is failing because the inputs to plot do not meet the input specification for this function. From plot, we see that a call with multiple inputs is given by
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn)
The 'DisplayName' doesn't seem to be one of those line spec properties. Note that even the simpler
plot(x1, y1,'o', 'DisplayName', sprintf('x1-y1; i-10:i; i=%d', i), x2, y2,'x')
generates the same error.
Since the original code worked, why not combine it with the sprintf as
legend(sprintf('x1-y1; %d:%d',i-10,i),sprintf('x2-y2; %d:%d',i+1,i+11), 'interpolated');
  1 件のコメント
Raj Raj
Raj Raj 2014 年 10 月 5 日
Thanks for you input. It worked..

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

その他の回答 (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