accessing tables saved in sequence.
17 views (last 30 days)
Show older comments
Hi,
Please don't suggest fandom page. As I been there already and still have a query. thank you for understanding.
I have ten .csv files with 6 column and 53496 rows, I have read each file from the folder as tables and named each table as t1,t2,...t10. Now I want to create a loop where the loop can access all the 10 tables.
I am trying as t{i}.cloumn1 but this is not working. Can anyone give me any suggestions.
Thank you
1 Comment
Stephen23
on 24 Aug 2020
Edited: Stephen23
on 24 Aug 2020
"I have ten .csv files with 6 column and 53496 rows"
Import them will be easy using a loop, just follow the MATLAB documentation:
"I have read each file from the folder as tables and named each table as t1,t2,...t10"
Numbering variables is not a good approach. Numbering variables like that will make acessing your data slow, complex, buggy, and difficult to debug. The recommended way to import data is to use one variable (e.g. a cell array) and basic indexing.
"Now I want to create a loop where the loop can access all the 10 tables."
If you imported your data using the recommended appraoch then that would be trivial:
"I am trying as t{i}.cloumn1 but this is not working. Can anyone give me any suggestions."
Don't force pseudo-indices into variable names. Import your data into one array using real indices.
Answers (1)
Mohammad Sami
on 24 Aug 2020
Edited: Mohammad Sami
on 24 Aug 2020
There are a few options. No 1, if all ten files have the same type of data, you can concatenate them into one big table. No 2, store the table in a cell array instead of storing them as individual variables
if true
% assuming 2 files in the folder
alltables = cell(2,1);
Filelist = {'csv1.csv' 'csv2.csv'};
for i=1:length(Filelist)
alltables{i} = readtable(Filelist{i});
end
% access column 1 of tables
Col1tab1 = alltables{1}.Var1;
Col1tab2 = alltables{2}.Var1;
% make one big table
% only work if all tables have same column name and type
Bigtable = vertcat(alltables{:});
Bigtable.Var1;
end
0 Comments
See Also
Categories
Find more on Tables 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!