txtfile2cell(file,d​atatype,delimiter,n​umRowSkip,numColSki​p)

Versión 1.3.0.0 (2,84 KB) por Li Chen
Load Text File to a Cell Array (strings or double)
161 descargas
Actualizado 28 ene 2015

Ver licencia

function cellarray=txtfile2cell(file,datatype,delimiter,numRowSkip,numColSkip)
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
% txtfile2cell - Load Text File to a Cell Array (strings or double)
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
% cellarray=txtfile2cell(file,datatype,delimiter,numRowSkip,numColSkip)
% 1. Input
% - file The input filename (string).
% - datatype 'str' (default) or 'num'
% - delimiter '\t' (default) or ',' or ' '
% - numRowSkip n : the first n rows are skipped (i.e header) default=0
% - numColSkip n : the first n columns are skipped (i.e header) default=0
% 2. Output
% - cellarray
% 3. Example
% cellarray=txtfile2cell('filename.txt'); %cell array of strings
% cellarray=txtfile2cell('filename.txt','num','\t',1,1);%numeric matrix without row and column headers
% cellarray=txtfile2cell('filename.txt','str','\t',1,1);%cell array of strings without row and column headers
% Written by Li CHEN
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
%% Check input arguments
if nargin < 5
numColSkip=0; %default
if nargin<4
numRowSkip=0; %default
end
if nargin<3
delimiter='\t';%default
end
if nargin<2
datatype='str'; %default
end
end
if nargin>5
error('Too many input arguments!')
end
if ~ischar(file)
error('Input filename is not a string!');
end;
%% Open input file and generate the cell array
tic
fid = fopen(file,'rt');
%check the number of columns
row1 = fgetl(fid);
col_num=length(strsplit(row1,delimiter));
frewind(fid)
%format
if numColSkip>0
str1=repmat('%*s',1,numColSkip);
if strcmp(datatype,'str')
str2=repmat('%s',1,col_num-numColSkip);
elseif strcmp(datatype,'num')
str2=repmat('%f',1,col_num-numColSkip);
else
error('Wrong datatype input!');
end
formatStr=strcat(str1,str2);
else
if strcmp(datatype,'str')
formatStr=repmat('%s',1,col_num);
elseif strcmp(datatype,'num')
formatStr=repmat('%f',1,col_num);
else
error('Wrong datatype input!')
end
end
%scan text file
%disp('Scanning text file, wait a moment...')
cellarray=textscan(fid,formatStr,'Headerlines',numRowSkip,'delimiter',delimiter);
%generate output file
cellarray=[cellarray{1:end}];
if isempty(cellarray)
error('Wrong input arguments!')
end
%% Close output file.
fclose(fid);
toc

Citar como

Li Chen (2024). txtfile2cell(file,datatype,delimiter,numRowSkip,numColSkip) (https://www.mathworks.com/matlabcentral/fileexchange/49114-txtfile2cell-file-datatype-delimiter-numrowskip-numcolskip), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2014a
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux
Etiquetas Añadir etiquetas

Community Treasure Hunt

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

Start Hunting!
Versión Publicado Notas de la versión
1.3.0.0

(1) Cell array can be numeric or strings.
(2) The first n rows or columns can be skipped.
(3) Delimiter can be '\t' or ',' or ' '.

1.2.0.0

data import

1.1.0.0

clear temp row1 column_num formatStrar

1.0.0.0