Access and extract table array using for loop
Afficher commentaires plus anciens
I have this table
tt = edfread('example.edf')
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.
Réponses (3)
tt = edfread('example.edf')
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
4 commentaires
Edoardo
le 13 Avr 2023
tt = edfread('example.edf')
tt = timetable2table(tt);
tt.Properties.VariableNames = {'Time','ECG1','ECG2'};
%%% for large number of variables %%%
%J = 1
% fprintf(repmat('%s Data ',1,100) \n', char(tt.Properties.VariableNames(J+1)),char(tt.Properties.VariableNames(J+2)), ... so on)
fprintf('%s Data %s Data \n', char(tt.Properties.VariableNames(2)),char(tt.Properties.VariableNames(3)))
for k = 1:length(tt.ECG1)
fprintf('at %s is\n',tt.Time(k))
[cell2mat(tt.ECG1(k)) cell2mat(tt.ECG2(k))]
end
Edoardo
le 13 Avr 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)))
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
Ran Yang
le 13 Avr 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 commentaire
Edoardo
le 13 Avr 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')
for ii = 1:height(T)
for jj = 1:width(T)
V = T{ii,jj}{:}
end
end
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
