Plots and legend don't match

1 回表示 (過去 30 日間)
Mihir Tasgaonkar
Mihir Tasgaonkar 2022 年 6 月 27 日
コメント済み: Mihir Tasgaonkar 2022 年 6 月 28 日
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 Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by