Effacer les filtres
Effacer les filtres

How to append Nested cell array to .csv file

8 vues (au cours des 30 derniers jours)
Karthik
Karthik le 15 Avr 2024
Modifié(e) : Voss le 16 Avr 2024
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 commentaires
Stephen23
Stephen23 le 16 Avr 2024
Modifié(e) : Stephen23 le 16 Avr 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).
Karthik
Karthik le 16 Avr 2024
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');

Connectez-vous pour commenter.

Réponses (2)

Hassaan
Hassaan le 15 Avr 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
  1 commentaire
Karthik
Karthik le 16 Avr 2024
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

Connectez-vous pour commenter.


Voss
Voss le 16 Avr 2024
Modifié(e) : Voss le 16 Avr 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.

Community Treasure Hunt

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

Start Hunting!

Translated by