Plots and legend don't match

1 visualizzazione (ultimi 30 giorni)
Mihir Tasgaonkar
Mihir Tasgaonkar il 27 Giu 2022
Commentato: Mihir Tasgaonkar il 28 Giu 2022
Hi,
I have a FOR loop where I'm plotting two different graphs. I'm then specifying the legend entries outside the loop.
for jj=1:5
plot(app.AccTabTorqueProfile,app.Motor.(sprintf('SR%d',jj)).RPM, app.Motor.(sprintf('SR%d',jj)).Torque, app.Motor.(sprintf('SR%d',jj)).RPM, app.Motor.(sprintf('SR%d',jj)).Torque*-app.const.mot.regen_trq_fac,"Color",C{jj});
hold(app.AccTabTorqueProfile, 'on');
drawnow;
plot(app.AccTabPowerProfile,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR/1000,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR*-app.const.mot.regen_trq_fac/1000,"Color",C{ii});
hold(app.AccTabPowerProfile, 'on');
drawnow;
end
legend(app.AccTabTorqueProfile,"SR1","SR2","SR3","SR4","SR5");
legend(app.AccTabPowerProfile,"SR1","SR2","SR3","SR4","SR5");
This code gives the same colours to "SR1" and "SR2", same for "SR3" and "SR4", and a third color for "SR5". Can someone help me plot the same colors in the legend as well?
  1 Commento
Walter Roberson
Walter Roberson il 27 Giu 2022
plot(app.AccTabPowerProfile,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR/1000,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR*-app.const.mot.regen_trq_fac/1000,"Color",C{ii});
Is there a relationship between ii used to index here, compared to the jj that you are looping over ?

Accedi per commentare.

Risposta accettata

dpb
dpb il 27 Giu 2022
Modificato: dpb il 27 Giu 2022
Besides Walter's note that you're using ii in the second plot instead of jj,it would be cleaner/simpler to do something like
for jj=1:5
vname="SR"+jj; % use string class overloaded plus operator
plot(app.AccTabTorqueProfile, ...
app.Motor.(vname).RPM, app.Motor.(vname).Torque, ...
app.Motor.(vname).RPM, app.Motor.(vname).Torque*-app.const.mot.regen_trq_fac,...
"Color",C{jj},'DisplayName',vname);
if jj==1,;hold(app.AccTabTorqueProfile, 'on');end
drawnow;
plot(app.AccTabPowerProfile, ...
app.Motor.(vname).RPM,app.Motor.(vname).PWR/1000, ...
app.Motor.(vname).RPM,app.Motor.(vname).PWR*-app.const.mot.regen_trq_fac/1000, ...
"Color",C{jj},'DisplayName',vname);
if jj==1,;hold(app.AccTabPowerProfile, 'on');end
drawnow;
end
where I've also assumed the ii was a typo and changed it to jj
Making that change alone would undoubtedly fix the problem by making them consistent usage of color (not to mention fixing up the data to not be the same for all for the second plot since ii isn't varying in the above code).
  1 Commento
Mihir Tasgaonkar
Mihir Tasgaonkar il 28 Giu 2022
Thanks a lot for the response!
And yes, the 'ii' and 'jj' thing was a typo...

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by