Plot data from matrix with legend name

6 ビュー (過去 30 日間)
felix.b
felix.b 2020 年 4 月 17 日
コメント済み: felix.b 2020 年 4 月 20 日
Dear colleagues,
how can I plot data from a matrix and have the legend entries in it automatically, too?
To make my question easier to understand I attached a code snippet:
1) What works:
Matrix = [a, A_2, 0];
%% Plot
figure
hold on
for i=1:size(Matrix,1)
plot([0,Matrix(i,1)],[Matrix(i,2),Matrix(i,3)])
end
2) Waht I want to have:
Matrix = [a, A_2, 0, 'legend entry'];
%% Plot
figure
hold on
for i=1:size(Matrix,1)
plot([0,Matrix(i,1)],[Matrix(i,2),Matrix(i,3)])
legend(Matrix(i,4))
end
Can you please give me a hint?
Best regards
Felix

採用された回答

Mehmed Saad
Mehmed Saad 2020 年 4 月 17 日
Cells are for that purpose, see help on cell
Matrix = {a, A_2, 0, 'legend entry'};
Matrix =
1×4 cell array
To access matrix first complete cell
Matrix{1,1}
it will give you a
if you want the first element of a you can access it using
Matrix{1,1}(1)
  4 件のコメント
Mehmed Saad
Mehmed Saad 2020 年 4 月 18 日
figure
hold on
for i=1:size(Matrix,1)
plot([0,Matrix{i,1}],[Matrix{i,2},Matrix{i,3}])
end
legend(Matrix{:,4})
hold off
grid on
felix.b
felix.b 2020 年 4 月 20 日
Thank you very much, Muhammad! ( I don't know why I didn't see this solution myself.)

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by