How to write a for loop to iterate through subjects IDs and their associated reaction times and create histogram

12 views (last 30 days)
I have a data set of several thousand values.
There are 10 unique subj_indx, 1 through 10. Each one represents an experimental subject. Each subject has a bunch of reaction times, rt.
I want to create a for loop that will go through this dataset and generate a histogram of the reaction time data for each subject.
I don't know where to start other than:
for id = 1:length(subj_idx)%loop for each unique id value
%rtIdx = 1:length(rt)
currentid = rt(rtIdx);
rtIdx = 0;
and I'm not sure even that's correct.

Accepted Answer

Paul on 19 Sep 2022
Hi Rania,
Check out the splitapply function and workflow. Example with an array, but I think you can make it work with a table
x = [ones(100,1) rand(100,1);2*ones(100,1) rand(100,1)]; % example data
splitapply(@(x) histogram(axes(figure),x),x(:,2),findgroups(x(:,1)))
If you want to pretty-up the histograms, I think splitapply can be used with a user-defined function just as easily.

Sign in to comment.

More Answers (0)


Find more on Data Distribution Plots 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!