How to set the datasource of a histogram programmatically?

2 visualizaciones (últimos 30 días)
Raptrick
Raptrick el 27 de Feb. de 2018
Respondida: Raptrick el 13 de Jul. de 2018
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

Respuesta aceptada

Raptrick
Raptrick el 13 de Jul. de 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

Más respuestas (2)

Bhuvnesh Singh
Bhuvnesh Singh el 2 de Mzo. de 2018
Hello Raptrick,
Yes the YDataSource' property is not available for histogram object.
For a workaround you can follow the below link:
  1 comentario
Raptrick
Raptrick el 2 de Mzo. de 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

Iniciar sesión para comentar.


Adam Nekimken
Adam Nekimken el 13 de Jul. de 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

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by