Clear Filters
Clear Filters

How to sort data (multiple rows into one)

2 views (last 30 days)
I need to sort data from multiple rows in one row. We used multiple trials (= target_number) in an experiment and the software stored the target_type, number_picture, duration, cue_condition, current_cue_picture, etc. for each trial. I've colored below the data that belongs together to one trail:
I'm interested in the identification_button and identification_RT for each trial. So I guess the data must be sorted like:
Is there a way to sort it with MATLAB automatically?

Accepted Answer

Seth Furman
Seth Furman on 15 Mar 2021
This sounds like a good use for unstack.
For example, suppose we have the following table, consisting of 3 consecutive trials and the names "a", "b", "c" and "d".
t =
12×2 table
Name Value
____ _____
"a" 82
"b" 91
"c" 13
"d" 92
"a" 64
"b" 10
"c" 28
"d" 55
"a" 96
"b" 97
"c" 16
"d" 98
We can create a Trial variable for grouping as follows
trial = floor( (0:height(t)-1) / length(unique(t.Name)))' + 1;
>> t.Trial = trial
t =
12×3 table
Name Value Trial
____ _____ _____
"a" 82 1
"b" 91 1
"c" 13 1
"d" 92 1
"a" 64 2
"b" 10 2
"c" 28 2
"d" 55 2
"a" 96 3
"b" 97 3
"c" 16 3
"d" 98 3
and then unstack the Value variable using Name as our indicator variable and grouping the values by trial number
>> unstack(t,'Value','Name','GroupingVariables','Trial')
ans =
3×5 table
Trial a b c d
_____ __ __ __ __
1 82 91 13 92
2 64 10 28 55
3 96 97 16 98

More Answers (1)

Nadine
Nadine on 15 Mar 2021
Already answered it myself. =)
Extracted the values (right column, column 7) for each variable Name (column 6) and put all together in one table:
If there's an easier way -- just let me know!

Categories

Find more on Shifting and Sorting Matrices 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!