Plots and legend don't match

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-6-27
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 ?

请先登录,再进行评论。

采纳的回答

dpb
dpb 2022-6-27
编辑:dpb 2022-6-27
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 个评论
Mihir Tasgaonkar
Mihir Tasgaonkar 2022-6-28
Thanks a lot for the response!
And yes, the 'ii' and 'jj' thing was a typo...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by