How to sort data (multiple rows into one)

조회 수: 2 (최근 30일)
Nadine
Nadine 2021년 3월 15일
댓글: Nadine 2021년 3월 16일
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?

채택된 답변

Seth Furman
Seth Furman 2021년 3월 15일
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

추가 답변 (1개)

Nadine
Nadine 2021년 3월 15일
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!

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by