txtfile2cell(file,datatype,delimiter,numRowSkip,numColSkip)
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
Zitieren als
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. Abgerufen .
Kompatibilität der MATLAB-Version
Plattform-Kompatibilität
Windows macOS LinuxKategorien
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
Version | Veröffentlicht | Versionshinweise | |
---|---|---|---|
1.3.0.0 | (1) Cell array can be numeric or strings.
|
||
1.2.0.0 | data import |
||
1.1.0.0 | clear temp row1 column_num formatStrar |
||
1.0.0.0 |