Effacer les filtres
Effacer les filtres

Extract data with a loop cycle from structure

3 vues (au cours des 30 derniers jours)
Stefano Alberti
Stefano Alberti le 7 Juin 2020
Commenté : Ameer Hamza le 8 Juin 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

Réponse acceptée

Ameer Hamza
Ameer Hamza le 8 Juin 2020
Modifié(e) : Ameer Hamza le 8 Juin 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 commentaires
Stefano Alberti
Stefano Alberti le 8 Juin 2020
Modifié(e) : Stefano Alberti le 8 Juin 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 le 8 Juin 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().

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by