How to join matrices of different lengths into one mother matrix

Dear Matlab,
I have an EEG signal dataset with 14 files.
GOAL: to join all files together into one mother uber matrix, i.e. similar to 'concatenate', so I can run stats.
The problem is (I think) that the files have different lengths. Some files are 5 minutes long, others are 10 minutes, etc. They have the same number of channels (i.e. 'rows'), but the datapoints, or 'columns' are different. How can I join them together? Please help. Thank you very much in advance. (Also I don't want to truncate to the shortest length, because I lose data).
  • Below is the code I have. (tried attahcing sample datafiles but too large)
************************************
close all; clear; clc;
% Get a list of data files ready to be analyzed
sublist = dir('*.mat');
sublist = {sublist.name};
% load in level-1 data
for subno=1:length(sublist)
load(sublist{subno})
% initialize matrices on 1st subject
if subno==1
GROUPsig = zeros([ length(sublist) size(dataFCP_pow2) ]);
end % end initialization loop
GROUPsig(subno,:,:) = dataFCP_pow2;
end % end loop around subjects

4 Comments

Your input data are 2-D matrices, right? What do you want as the output array? A single 1-D vector with all the data stitched together, or a 2-D array with each file in one row, or a cell array?
Have you tried scanning the 14 files first and then making a 2-D array with as many columns as the longest of them, then make another pass putting the data into the matrix? You'd have to keep track of the next column to write into after each file is put into the array.
Thanks very much for your response!
In the end, I would like it to be a: 14 X 9 X datalength/point (however long the datapoints are, columnwise).
so its SUBJECTS X #OFCHANNELS X DATALENGTH.
I havent tried scanning them (I'm unsure howto do that yet)
Can you crop down 2 or 3 files and attach them? But I agree with Paul below. It sounds like what you are suggesting would just complicate things for no reason. What do you plan on doing with this 3-D matrix once you've created it?
Thank you also for your reply! See below resp. to Paul. I tried to attach a sample zip file, but its too large. I'll keep trying

Sign in to comment.

 Accepted Answer

Hi Joanne,
If you want to store different size arrays in a single data structure you probably should consider using a container object like a struct or cell array. Here's an example using a cell array containing two matrices, each with two rows.
GROUPsig{1} = rand(2,3);
GROUPsig{2} = rand(2,4);
If you want to run the same function on each array, use cellfun. In this example, we compute the mean the rows
rowmeans = cellfun(@(x) mean(x,2),GROUPsig,'UniformOutput',false)
rowmeans = 1×2 cell array
{2×1 double} {2×1 double}
rowmeans is cell array where each cell contains a 2x1 vector containing the mean of each row in the matrix contained in the corresponding cell of GROUPsig
rowmeans{1}
ans = 2×1
0.9552 0.4208

7 Comments

Thank you so much for your help!
I tried your suggestion and replaced the last line of my code with: ...
GROUPsig{subno} = dataFCP_pow2;
and it worked, by joining all into a cell array or struct (see attached screenshot). (thank you so much!)
But now I don't know how to run the below pwelch code and to get the output of each pwelch 'run'. Do I create a for loop?
% How do I try the below on each cell(which has its own nested matrix)? Do I put it in a loop? How do I do that? (sorry for my amateurness!)
I tried to attach a sample zip file, but its too large. I'll keep trying
winsize = 2*srate; % 2-second window
hannw = .5 - cos(2*pi*linspace(0,1,winsize))./2;
nfft = srate*100;
[powspectC,hzC] = pwelch(GROUPsig,hannw,round(winsize/4),nfft,srate,'power');
use cellfun with 'uniform', 0
[spectcell, freqcell] = cellfun(@(sig) sig,hannw,round(winsize/4),nfft,srate,'power'), GROUPsig, 'UniformOutput', 0);
Thanks Walter for your reply. I tried your suggestion:
[spectcell, freqcell] = cellfun(@(GROUPsig) GROUPsig,hannw,round(winsize/4),nfft,srate,'power', GROUPsig, 'UniformOutput', 0);
But got this error:
"Error using cellfun
Input #2 expected to be a cell array, was double instead."
You'll need a transpose because pwelch treats the columns of the input as the channels. So I think it should be this (and pwelch needs to be called in anonymous function
[powspectC,hzC] = cellfun(@(sig) pwelch(sig.' ,hannw,round(winsize/4),nfft,srate,'power'), GROUPsig, 'UniformOutput', 0);
omg Paul, it worked (attached screenshot)! I'm so happy about it! Can't thank you enough!!
(Now I just have to figure out how to work with and index cell arrays for other stats operations but this is a great start. I'll post more ques. in separate thread on that if need)
Oh, right, I forgot the pwelch() part of the call! I must have mis-copied on my phone!
You're very welcome.
One of the most basic things about cell arrays to remember is that to process the object contained in the cell you need access it with brace {} indexing. Parentheses () indexing works on the cell array itself, not the objects contained in the cells. And, as we've seen, cellfun() can be a handy function.
Also, check out this post on comma separated lists by @Stephen23 for more ways to use cell arrays, particularly if you want to use the objects contained in a cell as input arguments to a function or if you want to have the outputs of a function mapped into elements of a cell array.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Asked:

on 5 Feb 2023

Commented:

on 5 Feb 2023

Community Treasure Hunt

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

Start Hunting!