Azzera filtri
Azzera filtri

Extract data with a loop cycle from structure

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Ameer Hamza
Ameer Hamza il 8 Giu 2020
Modificato: Ameer Hamza il 8 Giu 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 Commenti
Stefano Alberti
Stefano Alberti il 8 Giu 2020
Modificato: Stefano Alberti il 8 Giu 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 il 8 Giu 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().

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by