Draw edf plots using "for loop"

OK so this question is continuing my yesterday's question about accessing the edf table using for loop.
I have a table like this:
tt = edfread('example.edf')
tt = 6×2 timetable
Record Time ECG ECG2 ___________ _______________ _______________ 0 sec {1280×1 double} {1280×1 double} 10 sec {1280×1 double} {1280×1 double} 20 sec {1280×1 double} {1280×1 double} 30 sec {1280×1 double} {1280×1 double} 40 sec {1280×1 double} {1280×1 double} 50 sec {1280×1 double} {1280×1 double}
then I did several edf info extractions:
info = edfinfo('example.edf');
fs = info.NumSamples/seconds(info.DataRecordDuration);
after that I did the plot for the first record of the first signal, which refers to "EEG 0 sec" data:
recnum = 1;
signum = 1;
t = (0:info.NumSamples(signum)-1)/fs(signum);
y = tt.(signum){recnum};
plot(t,y)
or this one for the fifth record of the second signal, which refers to "EEG2 40 sec":
recnum = 5;
signum = 2;
t = (0:info.NumSamples(signum)-1)/fs(signum);
y = tt.(signum){recnum};
plot(t,y)
but for my case I would like to extract and plot all data:
  • "ECG" from 0 sec up to 50 sec
  • "ECG2" from 0 sec to 50 sec as well.
And for this case I would like to use "for loop" method. I tried to figure out what works in the loop but I totally have no idea. Can you help me to figure it out? Thank you

6 Comments

Is there an implication that the 1280 samples at time 0 are actually distributed between time 0 and time 10 ? If so then do you want to plot the groups for times 0, 10, 20, 30, 40, or do you want to plot the groups for time 0, 10, 20, 30, 40, 50, or do you want to plot the groups for times 0, 10, 20, 30, 40, plus the first sample from the group for time 50 ?
VBBV
VBBV on 14 Apr 2023
Edited: VBBV on 14 Apr 2023
Do you want to plot all data from 0 sec upto 50 sec as one continuous sample or in discrete samples in several plots ? did you try the for loop solution which i mentioned in your previous question ?
Edoardo
Edoardo on 15 Apr 2023
Edited: Edoardo on 15 Apr 2023
maybe as 1 continuous sample would be correct.
By the way, I have tried your code, and I would like to say thank you for your help in general.
In the other hand, what I would like to say is that if we do the reading towards the edf file, it will generates a timetable (or just table whatever), but in each cell it consist of matrix {1280x1 double}, and that is one that I don't understand in general
I mean, if the value is just a number, maybe it will become easier to do plotting, do "for loop", etc.
But I understand that this must be the result of edf file in general. I just somehow wondering how to interpret all these values just by using normal (human) intuition.
So if I'm following your code, the table "Data" will likely only kept the last record of the signals, which are in the "50 sec". What I want is that so all the data, from "0 sec" to "50 sec", both from "ECG" and "ECG2" will be kept in a distinct matrices, and I can use them later for the plotting.
What do you mean? By the way, you can refer to my reply towards @VBBV if you want to know what I would like to explain in this case.
Oh, if you look at this code
recnum = 1;
signum = 1;
t = (0:info.NumSamples(signum)-1)/fs(signum);
y = tt.(signum){recnum};
plot(t,y)
or this one
recnum = 5;
signum = 2;
t = (0:info.NumSamples(signum)-1)/fs(signum);
y = tt.(signum){recnum};
plot(t,y)
I use "recnum" and "signum" for the indexing, which signal and at what time the one that I will be used. But if I need to extract from more than two signal and from a large time interval, it will become a burden, since I need to repeat the "recnum" and "signum" commands over and over again. So that's the reason why I ask about the usage of "for-loop" in this case
According to your table, you have samples for each of 1280 different channels, with the samples taken every 10 seconds. According to your table, you do not (for example) have any data at time 1 second, or 2 seconds, and so on -- only at 0 seconds,10 seconds, 20 seconds, and so on.

Sign in to comment.

 Accepted Answer

VBBV
VBBV on 15 Apr 2023
Edited: VBBV on 15 Apr 2023
"Data" will likely only kept the last record of the signals, which are in the "50 sec",
Please look at the below code where whole data from 0 sec to 50 sec is stored.
tt = edfread('example.edf');
tt = timetable2table(tt);
tt.Properties.VariableNames = {'Time','ECG1','ECG2'};
%%% for large number of variables %%%
% fprintf(repmat('%s Data',1,100) \n', string(tt.Properties.VariableNames(2:end)))
fprintf('%s Data %s Data \n', string(tt.Properties.VariableNames(2:end)))
ECG1 Data ECG2 Data
for k = 1:length(tt.ECG1)
% fprintf('at %s is\n',tt.Time(k))
% access all data from table without inputting one by one
Data{k} = cell2mat(table2cell(tt(k,2:end))) ;
end
T = string(tt.Properties.VariableNames(2:end))
T = 1×2 string array
"ECG1" "ECG2"
%Rearrange data
Data = cell2mat(Data(1:end).');
for J = 1:length(string(tt.Properties.VariableNames(2:end)))
subplot(length(string(tt.Properties.VariableNames(2:end))),1,J)
plot(Data(:,J));title(T(J))
end
% store variable data in distinct matrices requires as many variables as
% Variablenames in table
D1 = Data(:,1) % All data of ECG from 0 sec to 50 sec
D1 = 7680×1
-0.1126 -0.0915 -0.0774 -0.0422 -0.0070 -0.0070 0.0352 0.0704 0.0845 0.1197
D2 = Data(:,2) % All data of ECG2 from 0 sec to 50 sec
D2 = 7680×1
-0.0070 -0.0070 -0.0070 -0.0000 -0.0000 -0.0070 -0.0000 0.0070 -0.0000 -0.0070
Could you tell why you want to store data in distinct variables for each table variable ?

1 Comment

Edoardo
Edoardo on 15 Apr 2023
Edited: Edoardo on 15 Apr 2023
I've followed yours (with some modification) and I think it's already worked. Thank you, anyway. Later if I don't understand I'll reach you again @VBBV
The reason why I want to store data in distinct variables because before I didn't know if the data can be combined using "cell2mat" command. Before I thought I need distinct variables then do the combination manually XD

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 14 Apr 2023

Commented:

on 16 Apr 2023

Community Treasure Hunt

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

Start Hunting!