How to append Nested cell array to .csv file
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 16 Apr. 2024
Bearbeitet: Stephen23
am 16 Apr. 2024
"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).
Antworten (2)
Hassaan
am 15 Apr. 2024
@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
am 16 Apr. 2024
Bearbeitet: Voss
am 16 Apr. 2024
"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 Kommentare
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!