Extract data with a loop cycle from structure

3 次查看(过去 30 天)
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

采纳的回答

Ameer Hamza
Ameer Hamza 2020-6-8
编辑:Ameer Hamza 2020-6-8
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 个评论
Stefano Alberti
Stefano Alberti 2020-6-8
编辑:Stefano Alberti 2020-6-8
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 2020-6-8
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().

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by