How to append Nested cell array to .csv file

조회 수: 4 (최근 30일)
Karthik
Karthik 2024년 4월 15일
편집: Voss 2024년 4월 16일
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
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).
Karthik
Karthik 2024년 4월 16일
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');

댓글을 달려면 로그인하십시오.

답변 (2개)

Hassaan
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
  댓글 수: 1
Karthik
Karthik 2024년 4월 16일
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

댓글을 달려면 로그인하십시오.


Voss
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.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by