필터 지우기
필터 지우기

How to set the datasource of a histogram programmatically?

조회 수: 7 (최근 30일)
Raptrick
Raptrick 2018년 2월 27일
답변: Raptrick 2018년 7월 13일
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

채택된 답변

Raptrick
Raptrick 2018년 7월 13일
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

추가 답변 (2개)

Bhuvnesh Singh
Bhuvnesh Singh 2018년 3월 2일
Hello Raptrick,
Yes the YDataSource' property is not available for histogram object.
For a workaround you can follow the below link:
  댓글 수: 1
Raptrick
Raptrick 2018년 3월 2일
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

댓글을 달려면 로그인하십시오.


Adam Nekimken
Adam Nekimken 2018년 7월 13일
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

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by