Clear Filters
Clear Filters

Extract data with a loop cycle from structure

3 views (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 8 Jun 2020
Edited: Ameer Hamza on 8 Jun 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 Comments
Stefano Alberti
Stefano Alberti on 8 Jun 2020
Edited: Stefano Alberti on 8 Jun 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 on 8 Jun 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().

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!