barh with colored bars: differences between 2012b and 2014b

3 ビュー (過去 30 日間)
D. benoy
D. benoy 2014 年 11 月 21 日
コメント済み: D. benoy 2014 年 11 月 24 日
hi,
I have a problem with the function barh when upgrading from MATLAB 2012b to MATLAB2014b.
I have the following function
function [ h ] = barh_colored(phi,cmin,cmax,loglinear)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
nbars2 = 64;
if cmax ~= -1
xbars=cmin:(cmax-cmin)/(nbars2-1):cmax;
[counts,x] = hist(phi, xbars);
else
[counts,x] = hist(phi, nbars2);
end
if strcmp(loglinear,'log')
h = barh(x,log10(counts),1);
else
h = barh(x,counts,1);
end
set(gca,'YAxisLocation','right');
ch = get(h,'Children');
fvd = get(ch,'Faces');
fvcd = get(ch,'FaceVertexCData');
cmap=get(gcf,'Colormap');
colormap(cmap);
for i=1:nbars2
fvcd(fvd(i,:))=i;
end
set(ch,'FaceVertexCData',fvcd)
if cmax ~= -1
ylim([cmin cmax]);
end
end
This code works fine with MATLAB 2012b. It generates plots like this:
Recently I switched to MATLAB 2014b and the code does not work anymore. Reason is that barh object does not have Children anymore so that:
ch = get(h,'Children');
returns an empty GraphicsPlaceHolder object.
How can I solve this?
Dany Benoy

採用された回答

Doug Hull
Doug Hull 2014 年 11 月 21 日
The Bar properties of Children is no longer available.
This is along the lines of what you want to accomplish:
bar([1 nan nan], 'facecolor',[1 0 0])
hold on
bar([nan 2 nan], 'facecolor',[0 1 0])
bar([nan nan 3], 'facecolor',[0 0 1])
hold off
  1 件のコメント
D. benoy
D. benoy 2014 年 11 月 24 日
Hi Doug,
Thanks for the answer. I upated my function to
function barh_colored(phi)
nbars2 = 64;
cmap=get(gcf,'Colormap');
[counts,x] = hist(phi, nbars2);
y = NaN * ones(1,nbars2);
for i=1:nbars2
y1 = y;
y1(i) = counts(i);
barh(x,y1,'BarWidth',1,...
'facecolor',cmap(i,:));
if i == 1,hold on, end
end
set(gca,'YAxisLocation','right');
end
Dany Benoy

サインインしてコメントする。

その他の回答 (1 件)

matt dash
matt dash 2014 年 11 月 22 日
I ran into this exact same problem. I solved it by writing my own bar function that constructs bars out of patches and wraps them with an hggroup, so the output is completely compatible with the way bar used to work. It's not very difficult, especially if you don't need any of the advanced features like stacking/grouping bars.
(Sorry i know this isn't the most useful answer, but something to think about)
I'm mostly a big fan of the new graphics, but bar seems to have really gone downhill. My real problem was the AWFULLY slow limit checking method that runs any time you resize a bar graph... nearly makes them unusable in large numbers (subplots etc).

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by