How to set the datasource of a histogram programmatically?

5 views (last 30 days)
Hello all,
Does anyone know how to set the datasource of a histogram?
figure;
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.
Patrick

Accepted Answer

Raptrick
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.
figure;
%generate data in struct
data.energy = randn(1000,1);
data.pulse = randn(1000,1);
%scatter plot
subplot(1,2,1)
hS = plot(data.energy,data.pulse,'g.');
hS.XDataSource = 'data.energy';
hS.YDataSource = 'data.pulse';
%histogramming
subplot(1,2,2)
hH = histogram(data.energy);
hH.Behavior.linked.YDataSource = 'data.energy';
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 (data.energy etc) from the base workspace.
Patrick

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
Raptrick on 2 Mar 2018
Bhuvnesh,
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;
data.energy = randn(100,1);
data.pulse = randn(100,1);
subplot(1,2,1)
hS = plot(data.energy,data.pulse,'g.');
hS.XDataSource = 'data.energy';
hS.YDataSource = 'data.pulse';
subplot(1,2,2)
hH = histogram(data.energy);
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 data.energy 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?
Patrick

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:
open(linkedFig.fig)
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.
Adam

Community Treasure Hunt

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

Start Hunting!