Putting subplots into a for loop?

Hello,
I'm looking to put these subplots into a for loop (I just provided the first 3 as an example but there are 7 total).
key1_ind_block1 = SRTT.RT(find(SRTT.Correct_Key(1:100)==1));
subplot(7,1,1)
plot(key1_ind_block1,'-o')
title('Block 1')
xlabel('Number of appearances')
ylabel('Reaction Time')
xlim([0 30])
key1_ind_block2 = SRTT.RT(find(SRTT.Correct_Key(101:200)==1));
subplot(7,1,2)
plot(key1_ind_block2,'-o')
title('Block 2')
xlabel('Number of appearances')
ylabel('Reaction Time')
xlim([0 30])
key1_ind_block3 = SRTT.RT(find(SRTT.Correct_Key(201:300)==1));
subplot(7,1,3)
plot(key1_ind_block3,'-o')
title('Block 3')
xlabel('Number of appearances')
ylabel('Reaction Time')
xlim([0 30])
I started with something like this but I know I'm missing something and it's giving me the error: "Error using find. Second argument must be a positive scalar integer."
for k = 1:7
subplot(7,1,k);
plot(SRTT.RT(find(SRTT.Correct_Key((1 + (k-1)*100):k * 100),'-o')));
end
If someone could provide help it would be much appreciated! (I'm newer to Matlab so let me know if you need more info).
Thanks in advanced.

Answers (1)

plot(SRTT.RT(find(SRTT.Correct_Key((1 + (k-1)*100):k*100))), '-o')

6 Comments

Thanks so much Walter.
This helped except all 7 plots are comign out exactly same as opposed to looping through each 100 points of data.
I think I should add something like this:
nkeys=1:numel(SRTT.Correct_Key);
for k = 1:7
subplot(7,1,k);
plot(nkeys(SRTT.RT(find(SRTT.Correct_Key((1 + (k-1)*100):k*100)))), '-o');
end
But with this I'm getting the error "Array indices must be positive integers or logical values".
Any idea how to fix that?
Whatever it is that SRTT.RT holds, it is not strictly positive integers suitable as indices for nkeys
Are you trying to plot SRTT.RT at correct keys on the y axis, with the relative index of the correct keys on the x axis?
X axis is number of occurances a key (Correct_Key) occured, and y axis is the reaction time for when the key was pressed (it was a computer task done with a key board and we're trying to see if certain keys yeilded a faster reaction time than others).
So yes, SRTT.RT (the reaction times of each key press) aren't strictly postive integers they're all fractions of a second.
Does SRTT.CorrectKey contain logical values (true, false), or does it contain numeric 0 and 1, or does it contain 0 (not correct) and something non-zero (correct), or does it contain counts of the number of times a particular key was pressed correctly?
Is it correct that SRTT.CorrectKey is information about keys in sequence of time? Because if so then it would not be number of occurances. It would make sense for SRTT.RT to be the reaction time in sequence of time; if SRTT.CorrectKey had to do with aggregate counts then you would need to know whether the corresponding SRTT.RT entries was max() of the reaction times for that choice of keys, or min() of reaction times for that choice of keys, or median() of reaction times for that choice of keys.
It seems like I got it working with this:
nkeys=SRTT.RT(find(SRTT.Correct_Key));
for k = 1:7
subplot(7,1,k);
plot(nkeys((1 + (k-1)*100):k*100), '-o');
end
Except my xaxis is 0-100 for every subplot instead of increasing to 101-200, 201-300, etc. Is there a way to add this in to the loop?
I appreciate your time helping me.
idx = 1 + (k-1)*100 : k*100;
plot(idx, nkeys(idx), '-o')

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 25 Mar 2019

Commented:

on 30 Mar 2019

Community Treasure Hunt

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

Start Hunting!