How to determine if an axes already has a colorbar?

8 ビュー (過去 30 日間)
David K
David K 2024 年 3 月 5 日
回答済み: Voss 2024 年 3 月 5 日
I would like to determine if an axes already has a colorbar. I have code which plots many objects on the same axes, but I only want to create and define the colorbar once to make things more efficient. I've tried looking for a ColorBar object as a child of the figure handle, but I have multiple subplots in the same figure, and each may or may not need their own colorbar. Is there a way to determine if a particular axes has a colorbar associated with it?

採用された回答

Voss
Voss 2024 年 3 月 5 日
There is a hidden axes property called 'Colorbar', which you can use.
% two axes, one with colorbar, one without
ax = subplot(2,1,1);
plot(1:10)
colorbar()
ax(2) = subplot(2,1,2);
plot(1:10)
ax1_has_colorbar = ~isempty(ax(1).Colorbar)
ax1_has_colorbar = logical
1
ax2_has_colorbar = ~isempty(ax(2).Colorbar)
ax2_has_colorbar = logical
0

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by