Want to writetable to tables stored in a cell array.
18 views (last 30 days)
Show older comments
Hi,
I'm in the process of taking a large table of data (419529x6, named 'TableData') and splitting it into smaller tables. I want to then write these tables to .csv (using writetable), however the code i've written which creates the tables stores them in a cell array (9x1). Please see below:
% Determines the total number of samples (rows)
Tot_Rows = height(TableData);
% Number of samples per table
N_Samples = 50000
% Create an array of samples per table over total number of samples
v = 1:N_Samples:Tot_Rows;
SplitTable = cell(numel(v) - 1,1);
for k = 2:numel(v)
SplitTable{k-1} = TableData(v(k-1):v(k) - 1,:);
end
SplitTable{k} = TableData(v(end):end,:);
This then stores 9 tables (a split of TableData) into a 9x1 cell array named 'SplitTable'. I would now like for these individual tables to be written to .csv, all named something similar, perhaps:
SplitTableData_1.csv
SplitTableData_2.csv
SplitTableData_3.csv
...
...
SplitTableData_9.csv
I'm guessing a loop will be required for this to happen, however i'm still relatively new to MATLAB and am struggling to work a solution out.
I'm aware splitting data isn't ideal. This is for a specific piece of software which would benefit taking smaller datasets and merging them.
Any help/ suggested solutions will be greatly appreciated.
Thanks in advance!
CS.
0 Comments
Answers (1)
Neeraj Kulkarni
on 15 Sep 2020
Hi Craig,
Yes, you can use a for loop to iterate over the cell array and write individual data into a .csv . You can you use writematrix() to write arrays present in each cell to the file.
Below is a sample code snippet to give an example of how it can be done:
%strings for creating file name
fName = 'SplitTableData';
ext = '.csv';
for x = 1:k %k = 9
fNo = num2str(x);
fName_final = append(fName,'_',fNo,ext);
Data2Write = (SplitTable{x});
writematrix(Data2Write,fName_final);
end
0 Comments
See Also
Categories
Find more on Environment and Settings 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!