Help sorting out for loop/if statement code to create a figure
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to plot vectors 8 through 38 but I only want them plotted if the vector length is greater than or equal to 30 -how can I fix my code?
fig2=figure (2)
c=0
for i=8:1:38
for t=1:1:numel(SpikesAll(i))
if isnan(SpikesAll(t,i))
break
else
c=c+1
end
if c>=30
subplot(8,5,i-7)
plot(days(:,1), SpikesAll(:,i))
end
end
end
0 comentarios
Respuestas (1)
Bob Thompson
el 6 de Mayo de 2019
fig2=figure (2)
c=0
for i=8:1:38
for t=1:1:numel(SpikesAll(i))
if isnan(SpikesAll(t,i))
break
else
c=c+1
end
if c>=30
subplot(8,5,i-7)
plot(days(:,1), SpikesAll(:,i))
end
end
end
When using for loops, the index is automatically advanced by 1, so specifying an interval of 1 is unnecessary.
What is the point of the second loop? It seems like you're looking for the number of elements which are not NaN, but you initially look at the number of elements at index i numel(SpikesAll(i)). This type of indexing seems to indicate that SpikesAll(i) is a cell with a matrix. If this is true, and you are looking for the number of elements inside of cell i, then you need to use curly braces after the convention of cell indexing, numel(SpikesAll{i}). If SpikesAll is not a cell array then t will always be 1, because numel() of a single element will always be 1.
You have two if conditions within the second for loop. The first appears to be intended to be coupled with the for loop to look for each element which ~isnan. Depending on how your data is structured this could all be grossely simplified. I think you are looking to plot two columns of data, the number of days, and the number of SpikesAll elements from column (i). If this is the case then you can remove your internal for loop, and the first if statement with the isnan command and length.
for i = 8:38
tmp = ~isnan(SpikesAll(:,i));
if length(tmp) >= 30
subplot(8,5,i-7)
plot(days(:,1), tmp)
end
end
The second should accomplish what you are looking for although again, you should be using cell indices on SpikesAll if this is a cell array. I have made a minor adjustment to the logic check in the above suggestion, but you what you had should have worked. If it was not working, please describe in greater detail what specifically was not being produced correctly.
0 comentarios
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!