Coverting Timetable to Column Vector

17 views (last 30 days)
Sam
Sam on 16 Nov 2022
Commented: Sam on 1 Dec 2022
Hi everyone,
I am working with .edf files and am using edfread, which is giving me a timetable output, when I would like a column vector.
We are recording voltage data in 1-hour increments at a sampling frequency of 1 KHz with 6 channels, each channel is unique.
I am using the option 'DataRecordOutputType' set to 'vector', which just makes this weirder.
Here's the code:
data = edfread('filename.edf','DataRecordOutputType' ,'vector');
and the output is:
I can solve this via doing the following:
data = edfread('filename.edf','DataRecordOutputType' ,'vector');
abf_like = [];
new_data = cell2mat(data{1,1});
abf_like = new_data;
for i = 2:size(data, 1)
new_data = cell2mat(data{i,1});
abf_like = vertcat(abf_like, new_data);
end
Which outputs my desired array (3600500 x 1). But this takes forever (understandably so), and there are 6 channels per edf file and many files. What am I missing with edfread, or what can I do to speed this up?
Thanks for any insight you can offer.
edit: here's something that works for what I need. Still would like some alternatives, if there are any:
clear all; close all; clc
edf = edfread('filename.edf');
abf_like = edf_to_abf(edf);
function out = edf_to_abf(edf)
edf = timetable2table(edf); edf = table2cell(edf);
edf = edf(:, 2:end);
abf_like = zeros(3600500,1);
abf_like = table(abf_like);
for j = 1:6
single_data = edf(:,j);
abf_like{:,j} = unpacker(single_data);
clear single_data
end
out = table2array(abf_like);
end
function out = unpacker(data)
abf_like(:,1) = cell2mat(data(1,1));
r = 501;
for i = 2:size(data, 1)
abf_like(r:r+499,1) = cell2mat(data(i,1));
r = r + 500;
end
out = abf_like;
end

Accepted Answer

Peter Perkins
Peter Perkins on 30 Nov 2022
Sam, it looks like you are ending up with a 7201x6 timetable whose variables are cell arrays, and each cell contains a 500x1 double vector. And it sounds like you want six (or one?) 7201*500x1 vector.I can't speak to how you are ending up with that timetable without seeing the file, but getting that should not take a long time:
>> tt = timetable(Size=[7201 2],VariableTypes=repmat("cell",1,2),StartTime=seconds(0),TimeStep=seconds(.5));
>> tt{:,:} = {rand(500,1)}
tt =
7201×2 timetable
Time Var1 Var2
__________ ______________ ______________
0 sec {500×1 double} {500×1 double}
0.5 sec {500×1 double} {500×1 double}
1 sec {500×1 double} {500×1 double}
1.5 sec {500×1 double} {500×1 double}
2 sec {500×1 double} {500×1 double}
2.5 sec {500×1 double} {500×1 double}
: : :
3597.5 sec {500×1 double} {500×1 double}
3598 sec {500×1 double} {500×1 double}
3598.5 sec {500×1 double} {500×1 double}
3599 sec {500×1 double} {500×1 double}
3599.5 sec {500×1 double} {500×1 double}
3600 sec {500×1 double} {500×1 double}
Display all 7201 rows.
>> tic, var1 = vertcat(tt.Var1{:}); toc
Elapsed time is 0.030527 seconds.
>> size(var1)
ans =
3600500 1
  3 Comments
Peter Perkins
Peter Perkins on 1 Dec 2022
varfun to the rescue!
>> tt2 = varfun(@(varN)vertcat(varN{:}),tt,OutputFormat='table')
tt2 =
3600500×2 table
Fun_Var1 Fun_Var2
________ ________
0.86119 0.86119
0.97724 0.97724
0.44452 0.44452
0.034243 0.034243
0.52377 0.52377
0.031639 0.031639
: :
0.21357 0.21357
0.43393 0.43393
0.033077 0.033077
0.92932 0.92932
0.10604 0.10604
0.26629 0.26629
Display all 3600500 rows.
(The two vars are the same because I was lazy when creating the test data.) And then
>> X = tt2.Variables;
>> X(1:5,:)
ans =
0.86119 0.86119
0.97724 0.97724
0.44452 0.44452
0.034243 0.034243
0.52377 0.52377
Sam
Sam on 1 Dec 2022
@Peter Perkins You are absolutely amazing! Thank you very much. I never would have figured that out.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!