EDF writing - error when using cell array as signal DATA
11 views (last 30 days)
Show older comments
Hello :-)
I would like to do a edf file entering my signals from a cell array (bin of time x EEG channels). Into each cells I have a vector containting 1000 values (mV across time). See picture:
However, I do get a very surprising error :
Error using signal.internal.edf.write.validateSignalData
Expected input to be a vector.
Error in signal.internal.edf.write.getFileCreateOpts>checkSignals (line 223)
signal.internal.edf.write.validateSignalData(sigData, ...
Error in signal.internal.edf.write.getFileCreateOpts>createHdr (line 96)
[reqhdr, sigData] = checkSignals(reqhdr, sigData, tNumSignals);
Error in signal.internal.edf.write.getFileCreateOpts>createAndValidateHeader (line 47)
[hdr, sigData, tsal] = createHdr(thdr, sigData, tsal, hdrPropertiesList, ...
Error in signal.internal.edf.write.getFileCreateOpts (line 13)
[hdr, sigData, tsal] = createAndValidateHeader(hdr, sigData, tsal, ...
Error in edfwrite/createFile (line 1620)
signal.internal.edf.write.getFileCreateOpts(hdr, signalData, ...
Error in edfwrite (line 499)
[obj, fileInfo] = createFile(obj, filename, hdr,...
It comes from this function in Matlab "source" code (function : validateSignalData ligne: 15):
validateattributes(signalData, {'cell'}, {'vector'});
I did check several times : Inside of each cells, I have a vector.
If I write :
isvector(data_cell{1,1})
ans =
logical
1
Valide for all cells
But if I ask :
validateattributes(data_cell, {'cell'}, {'vector'})
Expected input to be a vector.
Am I missing something because I do not see why my cell array would not have a vector attribute inside ?
My code when creating data_cell:
data_cell = {};
for nb_sig = 1: hdr.NumSignals
for time = 1:floor(length(LFP(1,:))/srate) % in sec
startpoint = ((time-1)*srate+1);
stoppoint = ((time-1)*srate+1) + srate;
temp_vect = LFP(nb_sig,startpoint:stoppoint);
data_cell{time, nb_sig} = temp_vect';
end
end
file_EDF_name = "R005_S1_V4.edf";
edfw = edfwrite(fullfile(pahtway_saving,file_EDF_name),hdr,data_cell);
Link for necessary variables :
2 Comments
Voss
on 5 Mar 2023
Please save all variables necessary to run the code (hdr, LFP, srate, etc.) into a .mat file and upload it using the paperclip button.
Accepted Answer
Voss
on 6 Mar 2023
Edited: Voss
on 6 Mar 2023
The edwrite documentation doesn't state this explicitly (that I can see), but apparently (according to this answer), if you specify a cell array as your signal data in edfwrite, it has to have one cell per signal.
That means, in this case, instead of a 900x11 cell array containing vectors of size 1001x1 (which should be 1000x1, by the way - your loops are duplicating an element because stoppoint is one more than it should be), you need a 1x11 (or 11x1) cell array containing vectors of size 900000x1. (I'm not sure why your LFP has 900092 columns instead of 900000, but you'd use only 900000 columns for 900 records with 1000 samples.)
Here's an example that uses only the first 50000 columns of your LFP (so the mat file would be small enough to upload).
load('R005_S1_V4_modified.mat')
t = (1:50000)/srate;
% plot 1st 4 signals for validation
figure()
subplot(2,2,1); plot(t,LFP(1,:)); title('Signal 1'); xlim([0 50]);
subplot(2,2,2); plot(t,LFP(2,:)); title('Signal 2'); xlim([0 50]);
subplot(2,2,3); plot(t,LFP(3,:)); title('Signal 3'); xlim([0 50]);
subplot(2,2,4); plot(t,LFP(4,:)); title('Signal 4'); xlim([0 50]);
% construct cell array
data_cell = cellfun(@transpose,num2cell(LFP,2),'UniformOutput',false)
% this modified data set has 50 records -> update hdr
hdr.NumDataRecords = 50;
% write edf file
pahtway_saving = '.';
file_EDF_name = "R005_S1_V4.edf";
edfw = edfwrite(fullfile(pahtway_saving,file_EDF_name),hdr,data_cell);
% read edf file
temp = edfread(file_EDF_name)
% plot 1st 4 signals for validation
PFC1 = vertcat(temp.PFC1{:});
PFC2 = vertcat(temp.PFC2{:});
PFC3 = vertcat(temp.PFC3{:});
BLA1 = vertcat(temp.BLA1{:});
figure()
subplot(2,2,1); plot(t,PFC1); title('PFC1'); xlim([0 50]);
subplot(2,2,2); plot(t,PFC2); title('PFC2'); xlim([0 50]);
subplot(2,2,3); plot(t,PFC3); title('PFC3'); xlim([0 50]);
subplot(2,2,4); plot(t,BLA1); title('BLA1'); xlim([0 50]);
5 Comments
Vijay
on 4 Oct 2024
Edited: Vijay
on 4 Oct 2024
Thank you very much.
I am a complete newbie at matlab, and the only reason I've downloaded a trial version (today, and with no real understanding of syntax) is to do exactly this task.
I have a misspecified spo2 value from polysomnography, which is preventing my edf file from being read by the usual viewer that my lab is familiar with. Because we couldn't get any technical assistance from anyone with more real expertise than me, I've been put on the spot.
This long preamble is to curry sympathy for what might be very stupid questions.
I have imported using edfread, concatenated the cells from the column, and recalculated the spo2 values but i can't get edfwrite to work
The data is in 25 channels with a uniform sampling rate of 500hz. Each element of the array is of class cell, and shows up as {500 x 1}.
From your answer above i understand that I'd need to convert each of the 25 channels to a single cell element, of dimensions 500*nrow x1?
Have i got that right? And is there readymade syntax that does this?
Extremely grateful for any help you can throw my way!
Voss
on 5 Oct 2024
I recommend you post a new question, upload your data, post your code, and explain the problem (e.g., any error messages).
https://www.mathworks.com/matlabcentral/answers/questions/new/?s_tid=gn_mlc_ans_ask
More Answers (0)
See Also
Categories
Find more on AI for Signals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!