paradox : updateing 'Data' property for histogram()

2 views (last 30 days)
N/A
N/A on 5 Aug 2016
Commented: Guillaume on 5 Aug 2016
I am trying to set the 'Data' property of a histogram once I plot it as follows:
figure(10);
h = histogram(4+randn(1000,1)); % plot histogram for first time for data~N(4,1)
h.Data = randn(1000,1); % update plot with new data ~ N(0,1)
The above approach updates the data, however does not change other related properties, as a result only a part of the samples are shown.
Any ideas how to update the histogram without having to plot it from scratch?
Cheers, M
  4 Comments
N/A
N/A on 5 Aug 2016
Edited: N/A on 5 Aug 2016
Hi Steven, I am using , Matlab 2015a. I have also replicated the same with Matlab 2016a as well
Cheers, M

Sign in to comment.

Answers (2)

N/A
N/A on 5 Aug 2016
Edited: N/A on 5 Aug 2016
clc;clear;figure(10);
% for reproducibility
rng('default');
N = 1000; x = 4+randn(N,1); % samples from x~N(4,1)
h = histogram(x);
% updating histogram()
xnew = randn(1000,1); % samples from x~N(0,1)
[~,BinEdges] = histcounts(xnew,h.NumBins);
BinLimits = [min(BinEdges),max(BinEdges)];
h.Data= xnew;
h.BinEdges= BinEdges;
h.BinLimits= BinLimits;

Steven Lord
Steven Lord on 5 Aug 2016
  • The Data property contains the actual data used to generate the histogram. You explicitly changed this, so it's not surprising that it is different.
  • The Values property is dependent on the Data and the locations of the bins, so it's not surprising that it is different. [As an extreme example, if I binned points based on their distance from Boston, Massachusetts in bins of 0-10, 10-20, 20-30, and 30-40 miles from Boston, I'd get VERY different results if I specified points in Massachusetts than if I specified points in California for those same bins.]
  • The Parent property is based on the axes that contains the histogram. The two histograms are in different subplot axes, so it's not surprising that it is different.
  • The Annotation property is a handle object, and the meaning of == for handle objects is slightly more restrictive than == for non-handle objects. Instead of asking "do each of your properties have the same values?" == for handles asks "do you refer to the same object?" The two histogram objects don't share the same object for their Annotation properties, so == returns false. But if you call isequal on the two handle objects from the Annotation properties (which asks the "do each of your properties have the same values?" question) you'll find that they are equal.
Based on your picture, I think I see what you want to do. You want the histogram object to recompute the bins based on the extent of the new data, right? If you change the BinMethod property of the second histogram to 'auto' (even though the value is already 'auto') "touching" that property will trigger the histogram object to recalculate its bin edges.
  1 Comment
Guillaume
Guillaume on 5 Aug 2016
Sounds like a Recalculate method would be a useful addition to the Histogram class, then.
More obvious than trying to find which property would trigger a recalculation.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!