How to set the datasource of a histogram programmatically?

11 views (last 30 days)
Hello all,
Does anyone know how to set the datasource of a histogram?
data = randn(10,1);
h = histogram(data);
linkdata on
h doesn't have a YDataSource property. It looks for me that you can only set the datasource manually in the figure.

Accepted Answer

Raptrick on 13 Jul 2018
Hi Adam,
I got some support on this topic. You can try the hidden/undocumented Behavior.linked.YDataSource property from HISTOGRAM handle.
%generate data in struct = randn(1000,1);
data.pulse = randn(1000,1);
%scatter plot
hS = plot(,data.pulse,'g.');
hS.XDataSource = '';
hS.YDataSource = 'data.pulse';
hH = histogram(;
hH.Behavior.linked.YDataSource = '';
hLD = linkdata('on');
hB = brush;
hB.Enable= 'on';
dpm = datamanager.linkplotmanager;
cellfun(@(x)x.close, {dpm.Figures.Panel});
This is how you can set the datasource of a histogram programmatically. Plot shows annotation in the right histogram when you brush the left plot by using the combination of brushing and linkdata.
My challenge is now to integrate this functionality in a UI or app It is challenging because linking data works only for variables ( etc) from the base workspace.

More Answers (2)

Bhuvnesh Singh
Bhuvnesh Singh on 2 Mar 2018
Hello Raptrick,
Yes the YDataSource' property is not available for histogram object.
For a workaround you can follow the below link:
  1 Comment
Raptrick on 2 Mar 2018
Thanks for answering my question. Your suggestion is not to use HISTOGRAM but HIST. The latter function is not recommended. HISTOGRAM possesses new features that I use for my analysis. At the same time I make use of data linking and data brushing.
I also notice that the returning handle of HISTOGRAM does not refer to any property that looks like a data source however the following code show that you can specify the data source of the histogram by hand.
figure; = randn(100,1);
data.pulse = randn(100,1);
hS = plot(,data.pulse,'g.');
hS.XDataSource = '';
hS.YDataSource = 'data.pulse';
hH = histogram(;
linkdata on
brush on
When you run this script the figure's datamanager complains about failing links. This is understandable because the histogram's datasource is not specified. By clicking edit...the data sources can be specified. In this dialog you can fill in in the empty field of the YDataSource of the histogram. By doing this:
  1. data brushing works across the histogram and the scatterplot
  2. data linking works with one variable data (struct) in the base workspace that can be modified
So yes, looks like that you can specify a datasource for the histogram. Only you can specify it by hand and not programmatically. Can you help me doing it programmatically?

Sign in to comment.

Adam Nekimken
Adam Nekimken on 13 Jul 2018
Hi Patrick,
I was having the same problem, and I think I found a workaround. First, run your script and do the manual linking, then save the figure. The next time you run the script, open the saved figure and call linkdata:
linkdata on
The figure retains the data link it had before, at least in my script. I've only tested this a bit, but it looks to have solved the problem for me.

Community Treasure Hunt

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

Start Hunting!