How can I update the histogram plot using LINKDATA function in MATLAB 7.8 (R2009a)?

10 views (last 30 days)
I generate two histogram plots that I would like to link to the data in my workspace:
x = -4:0.1:4;
y = randn(1000,2);
figure
subplot(2,1,1)
hist(y(:,1),x)
subplot(2,1,2)
hist(y(:,2),x)
I try to use LINKDATA but it does not link the data correctly because the data plotted on the histograms is part of a matrix.
linkdata on
y = randn(1000,2);
When I was using the PLOT command I was able to specify the 'YDataSource' of the plot and that would specify the data linking:
figure
plot(y(:,1),'YdataSource','y(:,1)')
linkdata on
y = randn(1000,2);
However, I cannot use the same approach for my problem because the histogram object does not seem to have the 'YDataSource' property. I would like to use the LINKDATA function and not the Data Linking tool in the figure toolbar.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Jul 2009
The 'YDataSource' property is not available for histogram object and hence it not possible to link it directly with LINKDATA.
To work around this issue, use the BAR function, which can be used to plot a histogram, and specify the 'YDataSource' for a BAR object.
This approach will require:
1. Computing vectors n and xout containing the frequency counts and the bin locations with the following command:
[n,xout] = hist(ydata,xdata) ;
2. Using bar(xout,n) to plot the histogram. For more information on using HIST and BAR functions together, please see the HIST function documentation by executing the following in the MATLAB command prompt:
doc hist
Consider the following example:
x = -4:0.1:4;
y = randn(1000,2);
[n,xout] = hist(y,x) ;
figure
subplot(2,1,1)
bar(xout,n(:,1),'YDataSource','n(:,1)')
subplot(2,1,2)
bar(xout,n(:,2),'YDataSource','n(:,2)')
%turn on datalinking
linkdata on
y = randn(1000,2);
% recalculate n
[n,xout] = hist(y,x) ;

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!