How can I fix this 2D graph and the histc figure ? (please see body description and problem)

1 view (last 30 days)
I am trying to create two figures. The first figure will be a plot of sin(x) and highlight the values above a threshold of .5. The second figure will be a histc that shows the number representation of values above that threshold. so if there are 5 values above .5 within second 0-1, it will show on the second graph as a dot on y=5 within 0-1s. I would like to match the bin ranges from these two figures. so the first figure has x-axis of 0,1,2,3,4 seconds, the second figure (which will be histc) will have x-axis of 0,1,2,3,4 that represents time intervals but the y-axis will show counts. this is my code so far,....if you run it, you will see that the counts in the second figure do not match with the graph from figure 1.
clear all
clc
THRESHOLD=0.5;
%t = 0:pi/100:6*pi;
t=0:pi/100:7;
y=sin(t);
P=[t' y'];
y_values=P(:,2);
Y=y_values>THRESHOLD;
Q=P(Y,:);
figure(1)
hold on
plot(P(:,1),P(:,2))
plot(Q(:,1),Q(:,2),'.r')
xlabel('time')
x=t;
binranges=(0:1:7);
BR= (binranges');
[bincounts, ind]=histc(x,binranges);
ind(:)=(ind');
counts=accumarray(ind(:), y(:) >=THRESHOLD);
figure
bar(BR,bincounts,'histc')
%plot(BR,bincounts,'+r');
plot((BR(1:end-1)+BR(2:end))/2, bincounts(1:end-1),'+r')
set(gca,'XTick',1:length(t));
set(gca,'YLim',[0, 40])
xlabel('bin ranges')
ylabel('Counts')

Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!