2

I have a matrix of data which I am trying to analyse. I have a data and I applied some processing part and I managed to get some information below a certain level as in trying to apply threshold to it. So after I applied threshold the data goes to 0 point. So I was wondering if there was a way to just eliminate the points without it leaving 0 in between. This is what the figure looks like with the zeros in that enter image description here I was trying to plot it without the gap the X Axis is time, y axis is amplitude. So will that be possible to just plot the events which are in blue and the time together?

%Find time
N = size(prcdata(:,1),1); 
t=T*(0:N-1)';
figure;
plot(t,U);
t1=t(1:length(t)/5);
X=(length(prcdata(:,4))/5);
a = U(1 : X);
threshold=3.063;
A=a>threshold;
plot_vals=a.*A;
figure; 
plot(t2,plot_vals1); %gives the plot which i added with this 

I also tried this code to club the events without the zeros but all it gives me is a straight line plot at 0.

%% Eliminate the rows and colomns which are zero
    B1=plot_vals1(plot_vals1 <= 0, :); 
    figure;
    plot(B1);

Also is there any way to take the scatter of the figure above? Will using scatter(t2,plot_vals1); work?

Matlaber
  • 35
  • 7

2 Answers2

2

If you want to only display those points that are above your threshold, you can use a logical index and set the value of the unwanted points to NaN:

threshold = 3.063;
index = (a <= threshold);
a(index) = NaN;
figure;
plot(t1, a);

Data points that are NaN simply won't be displayed, leaving a break in your plot. Here's a simple example that plots the positive points of a sine wave in red:

t = linspace(0, 4*pi, 100);
y = sin(t);
plot(t, y)
hold on;
index = (y < 0);
y(index) = nan;
plot(t, y, 'r', 'LineWidth', 2);

enter image description here

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • This `NaN` seems to be working... Thank you for that Is there any way to have it as a scatter plot? instead of using a linear plot too? – Matlaber Sep 12 '17 at 04:50
  • 1
    @Matlaber: Yes, just replace `plot` with `scatter`. – gnovice Sep 12 '17 at 04:51
  • As in scatter(t1,a); ? – Matlaber Sep 12 '17 at 04:53
  • 1
    @Matlaber: Yes. – gnovice Sep 12 '17 at 04:58
  • Also I was thinking if there is a way just to get the red curve and the corresponding time period (from your illustration) eliminating the blue curve and the times from 3 seconds to 6 seconds? say after the red plot ends at 2.5 seconds immediately following that we will have the red plot from 5.9-6 seconds? Can we do that in that code? – Matlaber Sep 12 '17 at 05:00
  • @Matlaber: Are you actually wanting to illustrate breaks in x-axis [as in this question](https://stackoverflow.com/q/37378487/52738), or do you want to simply remove the zero segments and collapse your vector, ignoring what the x-axis looks like as a result (i.e. just plotting versus vector index)? – gnovice Sep 12 '17 at 05:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154194/discussion-between-matlaber-and-gnovice). – Matlaber Sep 12 '17 at 05:08
1

So will that be possible to just plot the events which are in blue and the time together?

If the time of occurrence is not important to you, then the following will work:

After A=a>threshold;, change your code to

plot_vals=a(A);
figure; 
plot(plot_vals);

If the time of occurrence is important, then you can try setting the x-ticks and labels programmatically using the 'XTick' and 'XTickLabel' properties of your plot.

Get the corresponding time of interest like so:

t2=t1(A);

This should give you an idea of how to go about it using 5 equally spaced ticks:

xTickLabels = t2(floor(linspace(1,t2(end),5)));
xTicks = floor(linspace(1,numel(plot_vals),5));
plot(plot_vals);
set(gca,'XTick',xTicks,'XTickLabel',xTickLabels); % gca gets current axis handle

It is a bit of an art to determine which time points of interest you want, since your blue segments do not occur in equal sized clumps.

informaton
  • 1,462
  • 2
  • 11
  • 20
  • I will try the XTick, but in my matrix the Amplitude and Time are in different places will that still work with XTick? – Matlaber Sep 12 '17 at 05:06
  • Yes, this is essentially a hack that I am suggesting, but it is one I use for this reason. I'll update my answer with an example. – informaton Sep 12 '17 at 05:08
  • Ok thank you for that.. I am not sure as of yet how to use the XTick function – Matlaber Sep 12 '17 at 05:15
  • When I ran the code that you had suggested it returned an error saying Error using plot There is no XTick property on the Line class. Error in stackoverflow1 (line 77) plot(plot_vals,'XTick',xTicks,'XTickLabel',xTickLabels); – Matlaber Sep 12 '17 at 05:28
  • I'm so sorry; those are axes properties not line properties. It looks like you find the answer you want, but I'll correct the post anyway. – informaton Sep 13 '17 at 01:03
  • Thank you for the answer.. I just saw it now.. Tested it too and it seems to be working as well.. – Matlaber Sep 14 '17 at 23:09