How to append Nested cell array to .csv file
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I need to write data from multiple text files, which of the format Nested cell, to a single .csv file. Is there a easy method to do it.
I have tried fwrite, writecell, writematrix nothing helped.
Regards
Karthik
4 个评论
Stephen23
2024-4-16
编辑:Stephen23
2024-4-16
"Issue is i have 5000 such files which needs be read and appended to one single file."
Why is that an issue? Because you did not mention requiring any modifications to the file content the task should be easy: read the entire file as text (e.g. FILEREAD) and then write the content to a new text file (e.g. FPRINTF).
回答(2 个)
Hassaan
2024-4-15
@Karthik One of many possible approaches:
% Example nested cell array
nestedCell = {
{1, 2, 3}, 'Text';
4, {5, 'More Text'}
};
% Writing to CSV
writeNestedCellToCSV(nestedCell, 'output.csv');
function writeNestedCellToCSV(nestedCell, filename)
% Flatten the nested cell array
flatCell = flattenNestedCell(nestedCell);
% Write the flattened cell array to a CSV file
writecell(flatCell, filename);
end
function flatCell = flattenNestedCell(nestedCell)
numRows = size(nestedCell, 1);
flatCell = {}; % Initialize the output cell array
maxCols = 0;
% First, convert each row to a flat format and determine the maximum number of columns
tempCell = cell(numRows, 1); % Store each flattened row temporarily
for row = 1:numRows
flatRow = [];
for col = 1:size(nestedCell, 2)
if iscell(nestedCell{row, col})
% Flatten the inner cell by concatenating its contents
flatRow = [flatRow, nestedCell{row, col}];
else
% Directly append the data
flatRow = [flatRow, nestedCell{row, col}];
end
end
tempCell{row} = flatRow;
maxCols = max(maxCols, length(flatRow)); % Update the max columns found
end
% Now, ensure all rows have the same number of columns
for row = 1:numRows
currentLength = length(tempCell{row});
if currentLength < maxCols
% Pad with empty strings if less columns than the max
tempCell{row}(currentLength + 1:maxCols) = {''};
end
flatCell = [flatCell; tempCell{row}]; % Append to the final cell array
end
end
Voss
2024-4-16
编辑:Voss
2024-4-16
"I have attached the .txt file here which need to be read and written in a new file. Issue is i have 5000 such files which needs be read and appended to one single file."
This reads the files and writes all their contents into a single .txt file:
file_out = '.\combined.txt'; % absolute or relative path to the output file you want to create
F = dir('*.txt'); % modify '*.txt', if necessary, to have dir() return info about your 5000 text files
% read the files
names = fullfile({F.folder},{F.name});
for ii = 1:numel(F)
F(ii).data = fileread(names{ii});
end
% write the output file
fid = fopen(file_out,'w');
fprintf(fid,'%s\n',F.data);
fclose(fid);
If you want to write a .csv file containing the contents of cell arrays like you show in this comment, then please share the code you used to create that cell array.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!