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

버전 1.3.0.0 (2.84 KB) 작성자: Li Chen
Load Text File to a Cell Array (strings or double)
다운로드 수: 161
업데이트 날짜: 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