Clear Filters
Clear Filters

Access and extract table array using for loop

4 views (last 30 days)
Edoardo
Edoardo on 13 Apr 2023
Edited: VBBV on 14 Apr 2023
I have this table
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}
and I need to extract (or access) the data using "for loop". For example, from "ECG", we can extract the data from 0 sec, 10 sec, 20 sec, etc. without typing the code one-by-one manually. What should I do? Thank you.

Answers (3)

VBBV
VBBV on 13 Apr 2023
Edited: VBBV on 13 Apr 2023
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}
tt = timetable2table(tt);
tt.Properties.VariableNames = {'Time','ECG','EEG2'};
for k = 1:length(tt.ECG)
fprintf('ECG Data at %s is\n',[tt.Time(k)])
cell2mat(tt.ECG(k))
end
ECG Data at 0 sec is
ans = 1280×1
-0.1126 -0.0915 -0.0774 -0.0422 -0.0070 -0.0070 0.0352 0.0704 0.0845 0.1197
ECG Data at 10 sec is
ans = 1280×1
-0.2041 -0.2112 -0.1830 -0.1549 -0.1478 -0.1337 -0.0915 -0.0493 -0.0352 0.0423
ECG Data at 20 sec is
ans = 1280×1
-0.0281 -0.0493 -0.0352 -0.0352 -0.0563 -0.0704 -0.0704 -0.0985 -0.0774 -0.0563
ECG Data at 30 sec is
ans = 1280×1
-0.0633 -0.0845 -0.0845 -0.0915 -0.0915 -0.0985 -0.0845 -0.0985 -0.0985 -0.0985
ECG Data at 40 sec is
ans = 1280×1
-0.0774 -0.0493 -0.0281 0.0000 0.0282 0.0986 0.1267 0.1549 0.1971 0.2253
ECG Data at 50 sec is
ans = 1280×1
-0.0774 -0.0633 -0.0704 -0.0704 -0.0422 -0.0563 -0.0422 -0.0704 -0.0704 -0.0774
  4 Comments
Edoardo
Edoardo on 13 Apr 2023
I mean if I have something like this
how do I access all the data from ECG1 to ECG16 without inputting one-by-one?
VBBV
VBBV on 13 Apr 2023
Edited: VBBV on 14 Apr 2023
You can access all table data without inputting one by one as shown below
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 = cell2mat(table2cell(tt(k,2:end)))
end
at 0 sec is
Data = 1280×2
-0.1126 -0.0070 -0.0915 -0.0070 -0.0774 -0.0070 -0.0422 -0.0000 -0.0070 -0.0000 -0.0070 -0.0070 0.0352 -0.0000 0.0704 0.0070 0.0845 -0.0000 0.1197 -0.0070
at 10 sec is
Data = 1280×2
-0.2041 0.0559 -0.2112 0.0629 -0.1830 0.0559 -0.1549 0.0699 -0.1478 0.0489 -0.1337 0.0559 -0.0915 0.0489 -0.0493 0.0699 -0.0352 0.0839 0.0423 0.0559
at 20 sec is
Data = 1280×2
-0.0281 0.0280 -0.0493 0.0349 -0.0352 0.0280 -0.0352 0.0280 -0.0563 0.0210 -0.0704 -0.0000 -0.0704 -0.0000 -0.0985 0.0210 -0.0774 0.0210 -0.0563 0.0210
at 30 sec is
Data = 1280×2
-0.0633 -0.0140 -0.0845 -0.0280 -0.0845 -0.0280 -0.0915 -0.0210 -0.0915 -0.0140 -0.0985 -0.0280 -0.0845 -0.0140 -0.0985 -0.0280 -0.0985 -0.0070 -0.0985 -0.0280
at 40 sec is
Data = 1280×2
-0.0774 0.0349 -0.0493 0.0210 -0.0281 0.0210 0.0000 0.0280 0.0282 0.0349 0.0986 0.0210 0.1267 0.0210 0.1549 0.0280 0.1971 0.0280 0.2253 -0.0000
at 50 sec is
Data = 1280×2
-0.0774 -0.0280 -0.0633 -0.0419 -0.0704 -0.0070 -0.0704 -0.0000 -0.0422 -0.0000 -0.0563 -0.0000 -0.0422 -0.0000 -0.0704 -0.0070 -0.0704 -0.0070 -0.0774 -0.0140

Sign in to comment.


Ran Yang
Ran Yang on 13 Apr 2023
You can call everything in the ECG column using {:} and then concatenate it. Note the curly brackets.
data = cat(1, tt.ECG{:});
You can also specify a subset of rows (e.g. 0 sec, 20 sec, 40 sec) in the same way you would index a regular array.
subdata = cat(1, tt.ECG{1:2:5});
  1 Comment
Edoardo
Edoardo on 13 Apr 2023
I mean using the for loop command, so that for example I have the data with more than 60 seconds I can access it conveniently without writing the code one-by-one

Sign in to comment.


Stephen23
Stephen23 on 13 Apr 2023
"I need to extract (or access) the data using "for loop". For example, from "ECG", we can extract the data from 0 sec, 10 sec, 20 sec, etc. without typing the code one-by-one manually."
You can use curly-brace indexing:
Note that the example numeric data is nested in cell arrays in a table:
T = edfread('example.edf')
T = 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}
for ii = 1:height(T)
for jj = 1:width(T)
V = T{ii,jj}{:}
end
end
V = 1280×1
-0.1126 -0.0915 -0.0774 -0.0422 -0.0070 -0.0070 0.0352 0.0704 0.0845 0.1197
V = 1280×1
-0.0070 -0.0070 -0.0070 -0.0000 -0.0000 -0.0070 -0.0000 0.0070 -0.0000 -0.0070
V = 1280×1
-0.2041 -0.2112 -0.1830 -0.1549 -0.1478 -0.1337 -0.0915 -0.0493 -0.0352 0.0423
V = 1280×1
0.0559 0.0629 0.0559 0.0699 0.0489 0.0559 0.0489 0.0699 0.0839 0.0559
V = 1280×1
-0.0281 -0.0493 -0.0352 -0.0352 -0.0563 -0.0704 -0.0704 -0.0985 -0.0774 -0.0563
V = 1280×1
0.0280 0.0349 0.0280 0.0280 0.0210 -0.0000 -0.0000 0.0210 0.0210 0.0210
V = 1280×1
-0.0633 -0.0845 -0.0845 -0.0915 -0.0915 -0.0985 -0.0845 -0.0985 -0.0985 -0.0985
V = 1280×1
-0.0140 -0.0280 -0.0280 -0.0210 -0.0140 -0.0280 -0.0140 -0.0280 -0.0070 -0.0280
V = 1280×1
-0.0774 -0.0493 -0.0281 0.0000 0.0282 0.0986 0.1267 0.1549 0.1971 0.2253
V = 1280×1
0.0349 0.0210 0.0210 0.0280 0.0349 0.0210 0.0210 0.0280 0.0280 -0.0000
V = 1280×1
-0.0774 -0.0633 -0.0704 -0.0704 -0.0422 -0.0563 -0.0422 -0.0704 -0.0704 -0.0774
V = 1280×1
-0.0280 -0.0419 -0.0070 -0.0000 -0.0000 -0.0000 -0.0000 -0.0070 -0.0070 -0.0140

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!