How to append Nested cell array to .csv file

48 views (last 30 days)
Karthik
Karthik on 15 Apr 2024 at 14:27
Edited: Voss on 16 Apr 2024 at 16:18
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 Comments
Stephen23
Stephen23 on 16 Apr 2024 at 8:09
Edited: Stephen23 on 16 Apr 2024 at 8:20
"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).
Karthik
Karthik on 16 Apr 2024 at 8:27
Hi Stephen,
Was trying to read the frile from the network using uigetfile function and later read the file and write to a new file.
[file,location] = uigetfile('.txt','Choose TechLog Text File to load','P:\Transfix\Transfix Data finished units\DG900');
fid = fopen(strcat(location,file),'r');
out = fopen(fullfile('C:\Users\223086967\Desktop\Manufacturing_Projects\MATLAB Files', 'data.csv'), 'w');

Sign in to comment.

Answers (2)

Hassaan
Hassaan on 15 Apr 2024 at 16:36
@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
  1 Comment
Karthik
Karthik on 16 Apr 2024 at 10:55
Thank You Hassaan,
Tried with your code still im getting the error and not able to get data on file, my nested cell is of the below format with type cell

Sign in to comment.


Voss
Voss on 16 Apr 2024 at 16:13
Edited: Voss on 16 Apr 2024 at 16:18
"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.

Community Treasure Hunt

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

Start Hunting!