Reshape EEG data to wide format with new aggregate column names

1 view (last 30 days)
I'm using EEG lab to calculate relative spectral power for various bands. This went well, but now I'm stuck on how to reshape this the way I need it. I have 20 participants, and I want each participant to have only one row of data.
As such, I'm trying to convert this matrix so that the columns are a combination of electrode and frequency band. For example, each participant would have one row of data with columns labelled F7.relativeAlpha, F7.relativeBeta, and so on (see below for example). Any help would be greatly appreciated! I added an example of what I'm trying to create vs what I have currently.
I've tried unsuccessfully with the reshape function and with the unstack function.
RelativePowerWide = unstack(RelativePowerResults,'ID','RelativeDelta')

Accepted Answer

Voss
Voss on 28 Jan 2025
% a table
ID = ["01-02";"01-02";"01-02";"01-02";"01-02";"01-02";"01-02";"01-12";"01-12";"01-12";"01-12";"01-12";"01-12"];
Electrode = {'F7';'FP1';'FP2';'F8';'F3';'Fz';'F4';'F7';'FP2';'F8';'F3';'Fz';'F4'};
N = numel(ID);
RelativeDelta = rand(N,1);
RelativeTheta = rand(N,1);
RelativeBeta = rand(N,1);
RelativeGamma = rand(N,1);
T = table(ID,Electrode,RelativeDelta,RelativeTheta,RelativeBeta,RelativeGamma)
T = 13x6 table
ID Electrode RelativeDelta RelativeTheta RelativeBeta RelativeGamma _______ _________ _____________ _____________ ____________ _____________ "01-02" {'F7' } 0.99213 0.16194 0.097476 0.85428 "01-02" {'FP1'} 0.72373 0.16539 0.4748 0.57385 "01-02" {'FP2'} 0.61383 0.97623 0.0042034 0.28049 "01-02" {'F8' } 0.61888 0.095549 0.11584 0.70624 "01-02" {'F3' } 0.90665 0.29007 0.92064 0.5175 "01-02" {'Fz' } 0.18669 0.15658 0.57011 0.55038 "01-02" {'F4' } 0.26094 0.79279 0.25096 0.17227 "01-12" {'F7' } 0.21109 0.86385 0.066854 0.7987 "01-12" {'FP2'} 0.50538 0.31822 0.35951 0.59918 "01-12" {'F8' } 0.86902 0.94202 0.45049 0.30805 "01-12" {'F3' } 0.09369 0.28925 0.83403 0.72146 "01-12" {'Fz' } 0.81807 0.52419 0.43564 0.13657 "01-12" {'F4' } 0.4085 0.493 0.88088 0.10592
% unstack
Tnew = unstack(T,"Relative"+["Delta","Theta","Beta","Gamma"],'Electrode');
% adjust new variable names if needed
vars = Tnew.Properties.VariableNames(2:end);
vars = regexprep(vars,'(.*)_(.*)','$2.$1');
Tnew.Properties.VariableNames(2:end) = vars;
Tnew
Tnew = 2x29 table
ID F3.RelativeDelta F4.RelativeDelta F7.RelativeDelta F8.RelativeDelta FP1.RelativeDelta FP2.RelativeDelta Fz.RelativeDelta F3.RelativeTheta F4.RelativeTheta F7.RelativeTheta F8.RelativeTheta FP1.RelativeTheta FP2.RelativeTheta Fz.RelativeTheta F3.RelativeBeta F4.RelativeBeta F7.RelativeBeta F8.RelativeBeta FP1.RelativeBeta FP2.RelativeBeta Fz.RelativeBeta F3.RelativeGamma F4.RelativeGamma F7.RelativeGamma F8.RelativeGamma FP1.RelativeGamma FP2.RelativeGamma Fz.RelativeGamma _______ ________________ ________________ ________________ ________________ _________________ _________________ ________________ ________________ ________________ ________________ ________________ _________________ _________________ ________________ _______________ _______________ _______________ _______________ ________________ ________________ _______________ ________________ ________________ ________________ ________________ _________________ _________________ ________________ "01-02" 0.90665 0.26094 0.99213 0.61888 0.72373 0.61383 0.18669 0.29007 0.79279 0.16194 0.095549 0.16539 0.97623 0.15658 0.92064 0.25096 0.097476 0.11584 0.4748 0.0042034 0.57011 0.5175 0.17227 0.85428 0.70624 0.57385 0.28049 0.55038 "01-12" 0.09369 0.4085 0.21109 0.86902 NaN 0.50538 0.81807 0.28925 0.493 0.86385 0.94202 NaN 0.31822 0.52419 0.83403 0.88088 0.066854 0.45049 NaN 0.35951 0.43564 0.72146 0.10592 0.7987 0.30805 NaN 0.59918 0.13657

More Answers (0)

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!