Plot segments of an array with NaN using a for loop

1 visualizzazione (ultimi 30 giorni)
Hi,
I got an array with NaN. I want to plot, step-by-step, those segments of integers, that exceed a threshold and flanked by NaN, using a for loop.
Example:
Threshold=5
A=[NaN 1 2 3 4 4 3 1 NaN 2 4 5 10 11 12 14 13 11 7 5 3 NaN 2 3 7 15 20 25 17 13 9 5 1 NaN]
So, I want the loop to take the second segment of the integers flanked by NaN (2 - 3) and plot them. Then, I will do some operations with the plot, and I will continue with the loop using a wait(X) for the third segmentt (2 - 1).
The array always starts with a NaN, and finishes with an NaN.
for i = 1:length(A)
% code for getting the plot and make the operation
wait(next_plot)
end
At least, if I can plot the second and the third segment together skipping the first one, I would save my day.
Best,
Jose

Risposta accettata

dpb
dpb il 2 Mar 2021
Modificato: dpb il 2 Mar 2021
Rough outline to accomplish --
ix=find(isnan(A)); ix=ix(2:end); % find the NaN sections, ignore first
for i=1:numel(ix)-1 % over the groups
subplot(2,1,i); % just to show the separate pieces;
x=ix(i):ix(i+1); % set the x values to match
B=A(x); % select the portion wanted from A this group
B(B<=Threshold)=nan; % don't plot those <= Threshold
plot(x,B,'*-') % plot verus original ordinal position
end
If only want the values above the threshold, then delete those below (or select only those above) instead and the corresponding x values to match.
  2 Commenti
Jose Rego Terol
Jose Rego Terol il 7 Mar 2021
Many thanks for your comment.
I am using the first lines of the code
ix=find(isnan(A)); ix=ix(2:end); % find the NaN sections, ignore first
for i=1:numel(ix)-1
However, I am changing the rest because the rest of the code did not work for my code. I am usign ginput for each plot, therefore I cannot use subplots.
So, now I am doing like this but I got a problem to skip those segments that have lower values than the threshold (15)
ix=find(isnan(Trial(:,2))); ix=ix(1:end); % find the NaN sections, ignore first
for i=1:numel(ix)-1 % numel(ix)-1 over the groups
f=find(Trial((ix(1):ix(1+1)),2)>15); %Find all the values that are higher than 15 within this segment
%%% Here I want to set the criteria: If f is empty, disp no spike found, and if f
%%% has values, plot the entire segment (Trial)
f=logical(f);
if f == 0
disp('no spike')
else
plot(Trial((ix(3):ix(3+1)),1),Trial((ix(3):ix(3+1)),2))
end
pause()
% plot(i); % just to show the separate pieces;
% pause()
% x=ix(i):ix(i+1); % set the x values to match
% B=p(x); % select the portion wanted from A this group
% B(B<=Threshold)=nan; % don't plot those <= Threshold
% plot(x,B,'*-') % plot verus original ordinal position
end
The problem lies on the variable f. When the values are lower that 15, f is empty. Actually, I am looking for a code to say: in this segment or itineration, matlabdid not found values higher than 15. Then disp 'no spikes# and contiunue with the next segment.
Jose Rego Terol
Jose Rego Terol il 7 Mar 2021
Well, I got a workaround with numel()
for i=1:numel(ix)-1 % numel(ix)-1 over the groups
f=numel(find(Trial((ix(i):ix(i+1)),2)>15));
if f == 0
disp('no spike')
else
plot(Trial((ix(i):ix(i+1)),1),Trial((ix(i):ix(i+1)),2))
end
pause()
% plot(i); % just to show the separate pieces;
% pause()
% x=ix(i):ix(i+1); % set the x values to match
% B=Trial(x,2); % select the portion wanted from A this group
% B(B<=Threshold)=nan; % don't plot those <= Threshold
% plot(x,B) % plot verus original ordinal position
end
Now, if the segment does not have any value higher than 15, matlab disp 'no spike'.
Thanks for the first line. It push the code to do what I want to do.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Time Series Events in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by