function cell2txtfile(file,cell_array)
%%Written by Li CHEN on 22 Jan 2015, inspired by dlmcell %
%It is faster than dlmcell %
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> %%
% <><><><><> cell2txtfile - Write Cell Array to Text File <><><><><> %
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> %
% %
% 1. Input %
% - file The output filename (string). %
% - cell_array The cell array to be written. %
% %
% 2. Output %
% - filename.txt %
% 3. Example %
% cell2txt('filename.txt',cellarray); %
% %
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> %
%% Check input arguments
if nargin < 2
disp('Error - Give at least two input arguments!');
return;
elseif nargin > 2
disp('Error - Do not give more than 2 input arguments!');
return;
end
if ~ischar(file)
disp(['Error - File input has to be a string (e.g. ' ...
char(39) 'output.txt' char(39) '!']);
return;
end;
if ~iscell(cell_array)
disp('Error - Input cell_array not of the type "cell"!');
return;
end;
delimiter = '\t';
append = 'w';
%% Open output file
%tic
output_file = fopen(file,append);
[m,n]=size(cell_array);
%% Evaluate and write input array to the output file.
%disp('Writing the data to a txt file, wait a moment...')
for i=1:m
temp=strjoin(cell_array(i,:),delimiter);
fprintf(output_file,'%s\r\n',temp);
end
%% Close output file.
fclose(output_file);
%toc
Cite As
Li Chen (2024). cell2txtfile(file,cell_array) (https://www.mathworks.com/matlabcentral/fileexchange/49113-cell2txtfile-file-cell_array), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- MATLAB > Data Import and Analysis > Data Import and Export > Standard File Formats > Text Files >
- MATLAB > Language Fundamentals > Data Types > Cell Arrays >
Tags
Acknowledgements
Inspired by: dlmcell
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.