Adding a Legend with Variables as inputs

Im very new to MATLAB and Ive got a script which takes in an array determined by the user, does some calculations and then plots it on a graph with the x-axis being in the range chosen by the user. So, naturally the number of curves in my graph will be a variable so how do I get it to show what on the legend. This is what Ive got so far but it is not working. (lambda is a constant).
hold on
for r = (minr : incrr : maxr) %start the plotting
z = (r./lambda).*((r./lambda).^(r-1)).*(exp(-(x./r).^r)); %The equation
plot(x,z)
xlabel('x');
ylabel('f(x)');
title('GRAPH__1');
end
for i = minr : incrr : maxr
labelr = sprintf('r = %.2f',r);
end
for j = lambda
label_lambda = sprintf('lambda = %.2f',lambda);
end
legend(labelr,label_lambda);
hold off

1 件のコメント

Adam Danz
Adam Danz 2020 年 1 月 22 日
編集済み: Adam Danz 2020 年 1 月 22 日
@JDM_master, I've formatted your code so that it's correctly indented. To easily smart-indent you code from the Matlab Editor, highlight all of it (ctrl+a) and smart indent (ctrl+i). Its so much more readable this way.

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

 採用された回答

Adam Danz
Adam Danz 2020 年 1 月 22 日
編集済み: Adam Danz 2020 年 1 月 22 日

1 投票

Try this.
hold on
for r = minr : incrr : maxr %start the plotting
z = (r./lambda).*((r./lambda).^(r-1)).*(exp(-(x./r).^r)); %The equation
plot(x,z,'DisplayName',sprintf('r = %.2f',r))
xlabel('x');
ylabel('f(x)');
title('GRAPH__1');
end
% I'm not sure what this section is about....
% for j = lambda
% label_lambda = sprintf('lambda = %.2f',lambda); end
% end
legend(); % no need for inputs if you're using DisplayName
hold off

7 件のコメント

Travis Malouf
Travis Malouf 2020 年 1 月 22 日
@Adam Danz beat me to it. Displayname is a great tool to add to your Matlab plotting knowledgebase
JDM_master
JDM_master 2020 年 1 月 22 日
Thanks Adam!!! That displays the values perfectly but it doesn't show the corresponding colours of the graph next to it. Any idea how to display the corresponding colours next to it?
Adam Danz
Adam Danz 2020 年 1 月 22 日
Really? That's hard to imagine. Could you add a screenshot of your figure? How does your code differ from the demo code?
JDM_master
JDM_master 2020 年 1 月 22 日
The code is not different from the demo code, I have attached the graph so you can see what it looks like. : )
Walter Roberson
Walter Roberson 2020 年 1 月 22 日
What does
opengl info
Show for you?
JDM_master
JDM_master 2020 年 1 月 22 日
opengl info
Version: '4.5.13572 Compatibility Profile Context 19.11.3 26.20.13031.18002'
Vendor: 'ATI Technologies Inc.'
Renderer: 'AMD Radeon(TM) Vega 8 Graphics'
RendererDriverVersion: '26.20.13031.18002'
RendererDriverReleaseDate: '15-Nov-2019'
MaxTextureSize: 16384
Visual: 'Visual 0x08, (RGBA 32 bits (8 8 8 8), Z depth 16 bits, Hardware acceleration, Double buffer, Antialias 8 samples)'
Software: 'false'
HardwareSupportLevel: 'full'
SupportsGraphicsSmoothing: 1
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
Extensions: {303×1 cell}
MaxFrameBufferSize: 16384
This is what it shows when I type that in
Adam Danz
Adam Danz 2020 年 1 月 22 日
Is the problem fixed after running this command?: opengl software

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

質問済み:

2020 年 1 月 22 日

コメント済み:

2020 年 1 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by