Average over duplicate values in xlsx file

조회 수: 1 (최근 30일)
rbhatt93
rbhatt93 2019년 4월 1일
답변: Andrei Bobrov 2019년 4월 2일
This is my first time using MATLAB so I have very very less knowledge of the syntax. I am stuck with a problem. I have a xlsx file which looks like:
I want to average the columns `Response`, `ResponseC`, `ResponseCo` and `Reaction_time` for repeating values of `StimuliName` and finally in the output have something like this:
Untitled.png
So basically I want the average values with only specific fields.
Can someone please guide me as to how I can get this result?
  댓글 수: 3
rbhatt93
rbhatt93 2019년 4월 2일
They are stored in a cell array with the headers.
Adam Danz
Adam Danz 2019년 4월 2일
OK; I added a comment under my answer. Let me know if you get stuck implementing the solution.

댓글을 달려면 로그인하십시오.

답변 (2개)

Adam Danz
Adam Danz 2019년 4월 1일
You can use findgroups() to split the rows into groups based on StimuliName. Then you can use splitapply() to apply a funciton (ie, mean) to a column of the data for each group.
% assign group numbers to simuli names in column 6
[stimGroups, groupID] = findgroups(data(:,6));
% Use splitapply() to perform stats on grouped data in columns 14, 15, and 16
meanVals1 = splitapply(@mean, [data{:, 14}]', stimGroups);
meanVals2 = splitapply(@mean, [data{:, 15}]', stimGroups);
meanVals3 = splitapply(@mean, [data{:, 16}]', stimGroups);
From here you can store the data in a table or cell array. The best choice depends on what format the data are already stored in matlab and how you plan on using the data.
  댓글 수: 1
Adam Danz
Adam Danz 2019년 4월 2일
If the cell array contains headers along the first row, you can easily adapt the code above to ignore the first row.
data(:, 6)
% should become
data(2:end, 6)

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2019년 4월 2일
T = readtable('yourfile.xlsx');
T_out1 = varfun(@(x)x(1),T,'I',1:14,'G','StimuliName');
T_out2 = varfun(@mean,T,'I',15:18,'G','StimuliName');
out = [T_out1(:,3:end), T_out2(:,3:end)];

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by