Plot legend information not showed properly

2 Ansichten (letzte 30 Tage)
WPG
WPG am 18 Nov. 2022
Beantwortet: Taylor am 5 Aug. 2025
I'm trying to plot a compare between two tables imported from excel and when the graph is ploted, the legend comes out wrong. Strange is that in matlab 2020 it works perfectly, but in 2014, the plot is as shown below. Why te sequence name is apearing just for the first one? Thanks
clear all
close all
clc
% Importing maps
map1=xlsread('Compare_Input.xlsx',1,'A1:R20');
Speeda=map1(1,2:18);
Pedal_0a=map1(2,2:18);
Pedal_2a=map1(3,2:18);
Pedal_4a=map1(4,2:18);
Pedal_8a=map1(5,2:18);
Pedal_12a=map1(6,2:18);
Peda1_16a=map1(7,2:18);
Pedal_20a=map1(8,2:18);
Pedal_24a=map1(9,2:18);
Pedal_28a=map1(10,2:18);
Pedal_32a=map1(11,2:18);
Pedal_38a=map1(12,2:18);
Pedal_44a=map1(13,2:18);
Pedal_50a=map1(14,2:18);
Pedal_62a=map1(15,2:18);
Pedal_74a=map1(16,2:18);
Pedal_86a=map1(17,2:18);
Pedal_100a=map1(18,2:18);
map2=xlsread('Compare_Input.xlsx',2,'A1:R20');
Speedb=map2(1,2:18);
Pedal_0b=map2(2,2:18);
Pedal_2b=map2(3,2:18);
Pedal_4b=map2(4,2:18);
Pedal_8b=map2(5,2:18);
Pedal_12b=map2(6,2:18);
Peda1_16b=map2(7,2:18);
Pedal_20b=map2(8,2:18);
Pedal_24b=map2(9,2:18);
Pedal_28b=map2(10,2:18);
Pedal_32b=map2(11,2:18);
Pedal_38b=map2(12,2:18);
Pedal_44b=map2(13,2:18);
Pedal_50b=map2(14,2:18);
Pedal_62b=map2(15,2:18);
Pedal_74b=map2(16,2:18);
Pedal_86b=map2(17,2:18);
Pedal_100b=map2(18,2:18);
[status,sheets] = xlsfinfo('Compare_Input.xlsx');
x=sheets(1:1);
y=sheets(2:2);
a=strcat(x,' Pedal 0%');
b=strcat(x,' Pedal 2%');
c=strcat(x,' Pedal 4%');
d=strcat(x,' Pedal 8%');
e=strcat(x,' Pedal 12%');
f=strcat(x,' Pedal 16%');
g=strcat(x,' Pedal 20%');
h=strcat(x,' Pedal 24%');
i=strcat(x,' Pedal 28%');
j=strcat(x,' Pedal 32%');
k=strcat(x,' Pedal 38%');
l=strcat(x,' Pedal 44%');
m=strcat(x,' Pedal 50%');
n=strcat(x,' Pedal 62%');
o=strcat(x,' Pedal 74%');
p=strcat(x,' Pedal 86%');
q=strcat(x,' Pedal 100%');
a1=strcat(y,' Pedal 0%');
b1=strcat(y,' Pedal 2%');
c1=strcat(y,' Pedal 4%');
d1=strcat(y,' Pedal 8%');
e1=strcat(y,' Pedal 12%');
f1=strcat(y,' Pedal 16%');
g1=strcat(y,' Pedal 20%');
h1=strcat(y,' Pedal 24%');
i1=strcat(y,' Pedal 28%');
j1=strcat(y,' Pedal 32%');
k1=strcat(y,' Pedal 38%');
l1=strcat(y,' Pedal 44%');
m1=strcat(y,' Pedal 50%');
n1=strcat(y,' Pedal 62%');
o1=strcat(y,' Pedal 74%');
p1=strcat(y,' Pedal 86%');
q1=strcat(y,' Pedal 100%');
%plot
figure
plot(Speeda,Pedal_0a,'r','LineWidth',2);
hold on
plot(Speeda,Pedal_2a,'r','LineWidth',2);
plot(Speeda,Pedal_4a,'r','LineWidth',2);
plot(Speeda,Pedal_8a,'r','LineWidth',2);
plot(Speeda,Pedal_12a,'r','LineWidth',2);
plot(Speeda,Peda1_16a,'r','LineWidth',2);
plot(Speeda,Pedal_20a,'r','LineWidth',2);
plot(Speeda,Pedal_24a,'r','LineWidth',2);
plot(Speeda,Pedal_28a,'r','LineWidth',2);
plot(Speeda,Pedal_32a,'r','LineWidth',2);
plot(Speeda,Pedal_38a,'r','LineWidth',2);
plot(Speeda,Pedal_44a,'r','LineWidth',2);
plot(Speeda,Pedal_50a,'r','LineWidth',2);
plot(Speeda,Pedal_62a,'r','LineWidth',2);
plot(Speeda,Pedal_74a,'r','LineWidth',2);
plot(Speeda,Pedal_86a,'r','LineWidth',2);
plot(Speeda,Pedal_100a,'r','LineWidth',2);
plot(Speedb,Pedal_0b,'g','LineWidth',2);
plot(Speedb,Pedal_2b,'g','LineWidth',2);
plot(Speedb,Pedal_4b,'g','LineWidth',2);
plot(Speedb,Pedal_8b,'g','LineWidth',2);
plot(Speedb,Pedal_12b,'g','LineWidth',2);
plot(Speedb,Peda1_16b,'g','LineWidth',2);
plot(Speedb,Pedal_20b,'g','LineWidth',2);
plot(Speedb,Pedal_24b,'g','LineWidth',2);
plot(Speedb,Pedal_28b,'g','LineWidth',2);
plot(Speedb,Pedal_32b,'g','LineWidth',2);
plot(Speedb,Pedal_38b,'g','LineWidth',2);
plot(Speedb,Pedal_44b,'g','LineWidth',2);
plot(Speedb,Pedal_50b,'g','LineWidth',2);
plot(Speedb,Pedal_62b,'g','LineWidth',2);
plot(Speedb,Pedal_74b,'g','LineWidth',2);
plot(Speedb,Pedal_86b,'g','LineWidth',2);
plot(Speedb,Pedal_100b,'g','LineWidth',2);
hold off
grid on
xlabel('Vehicle Speed');
ylabel('Pedal [%]');
legend(strcat(x,' Pedal 0%'), strcat(x,' Pedal 2%'), strcat(x,' Pedal 4%'), strcat(x,' Pedal 6%'), strcat(x,' Pedal 8%'), strcat(x,' Pedal 12%'), strcat(x,' Pedal 16%'), strcat(x,' Pedal 20%'), strcat(x,' Pedal 24%'), strcat(x,' Pedal 28%'), strcat(x,' Pedal 32%'), strcat(x,' Pedal 38%'), strcat(x,' Pedal 44%'), strcat(x,' Pedal 50%'), strcat(x,' Pedal 62%'), strcat(x,' Pedal 74%'), strcat(x,' Pedal 86%'), strcat(x,' Pedal 100%'), strcat(y,' Pedal 0%'), strcat(y,' Pedal 2%'), strcat(y,' Pedal 4%'), strcat(y,' Pedal 6%'), strcat(y,' Pedal 8%'), strcat(y,' Pedal 12%'), strcat(y,' Pedal 16%'), strcat(y,' Pedal 20%'), strcat(y,' Pedal 24%'), strcat(y,' Pedal 28%'), strcat(y,' Pedal 32%'), strcat(y,' Pedal 38%'), strcat(y,' Pedal 44%'), strcat(y,' Pedal 50%'), strcat(y,' Pedal 62%'), strcat(y,' Pedal 74%'), strcat(y,' Pedal 86%'), strcat(y,' Pedal 100%'));
legend('Location','northeastoutside')
title('Transitions Compare');
  4 Kommentare
the cyclist
the cyclist am 18 Nov. 2022
By the way, the fact that you are using dynamically named variables makes this code a nightmare to understand. You might want to read this guide to why dynamically named variables are almost always a terrible idea.
WPG
WPG am 18 Nov. 2022
I added the input data.
The "x" and "y" added to each legend line was a try to replace the variables a, b, c, d..... q created in the begining of the script " legend(strcat(x,' Pedal 0%'), strcat(x,' Pedal 2%'), strcat(x,' Pedal 4%')" but without success.
Thanks

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Taylor
Taylor am 5 Aug. 2025
I personally recommend using tables and the DisplayName argument of the plot function to help manage your legend.
pedalData1 = readData("Compare_Input.xlsx", 1);
pedalData2 = readData("Compare_Input.xlsx", 2);
figure;
hold on
for ii = 1:length(pedalData1.Properties.VariableNames)-1
color = lines(length(pedalData1.Properties.VariableNames)-1); % Generate distinct colors for each iteration
plot(pedalData1, "Speed", ii+1, "DisplayName", pedalData1.Properties.VariableNames{ii+1}, "LineStyle", "-", "Color", color(ii, :));
plot(pedalData2, "Speed", ii+1, "DisplayName", pedalData2.Properties.VariableNames{ii+1}, "LineStyle", "--", "Color", color(ii, :));
end
legend("Location", "bestoutside")
hold off
function pedalData= readData(excelSheet, sheetToRead)
speed = readmatrix(excelSheet, "Range", "B3:R3", "Sheet", sheetToRead);
pedalData = readmatrix(excelSheet, "Range", "B4:R20", "Sheet", sheetToRead)';
variableNameSuffixes = readmatrix(excelSheet, "Range", "A4:A20", "Sheet", sheetToRead);
pedalData = array2table(pedalData, 'VariableNames', "Pedal " + string(variableNameSuffixes) + "%");
pedalData = [array2table(speed', 'VariableNames', {'Speed'}), pedalData];
end

Kategorien

Mehr zu Graphics finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by