Azzera filtri
Azzera filtri

Want to writetable to tables stored in a cell array.

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

Risposte (1)

Neeraj Kulkarni
Neeraj Kulkarni il 15 Set 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.
Also, to iteratively create file names strings can be appended using append .
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

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by