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

版本 1.3.0.0 (2.8 KB) 作者: Li Chen
Load Text File to a Cell Array (strings or double)
161.0 次下载
更新时间 2015/1/28

查看许可证

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

引用格式

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. 检索来源 .

MATLAB 版本兼容性
创建方式 R2014a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Text Files 的更多信息
标签 添加标签

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
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