Extract data with a loop cycle from structure

3 visualizaciones (últimos 30 días)
Stefano Alberti
Stefano Alberti el 7 de Jun. de 2020
Comentada: Ameer Hamza el 8 de Jun. de 2020
Hi,
I need to extract data from structure iteratively as multiple tables.
The response with this code is: 'Unable to use a value of type cell as an index.'
Furthemore I'd like to plot the extracted data.
Many thanks,
Code:
directory = '/Users/Dati/';
S = dir(fullfile(directory,'*.csv'));
for k = 1:numel(S)
F = fullfile(directory,S(k).name);
S(k).data = readtable(F, 'PreserveVariableNames', true, 'ReadVariableNames', true);
end
for i = 1: numel(S)
out(i) = S({i}).data;
end

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 8 de Jun. de 2020
Editada: Ameer Hamza el 8 de Jun. de 2020
The correct syntax is
directory = '/Users/Dati/';
S = dir(fullfile(directory,'*.csv'));
for k = 1:numel(S)
F = fullfile(directory,S(k).name);
S(k).data = readtable(F, 'PreserveVariableNames', true, 'ReadVariableNames', true);
end
out = cell(1, numel(S))
for i = 1: numel(S)
out{i} = S(i).data;
end
You need to store the tables in a cell array.
Also, second for-loop is not needed. Following is equivalent
directory = '/Users/Dati/';
S = dir(fullfile(directory,'*.csv'));
for k = 1:numel(S)
F = fullfile(directory,S(k).name);
S(k).data = readtable(F, 'PreserveVariableNames', true, 'ReadVariableNames', true);
end
out = {S.data};
  4 comentarios
Stefano Alberti
Stefano Alberti el 8 de Jun. de 2020
Editada: Stefano Alberti el 8 de Jun. de 2020
Thanks again Ameer!
I'd like to extract data from the structure, with a dedicated table for each table inside structure.
After that for each column of each table I'd like to apply movmean.
Maybe it is not a good approach 'extract' data but directly apply the movmean to each column inside the structure.
Hope to be clear,
Thanks again.
Ameer Hamza
Ameer Hamza el 8 de Jun. de 2020
The data in the tables are not in numeric format. It is the form of character arrays. Also, there are several columns with text data. How do you decide which column do you want to apply movmean().

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by