String MATLAB:如何从.txt文件加载文件名列表

String MATLAB:如何从.txt文件加载文件名列表,string,matlab,import,String,Matlab,Import,filelist.txt包含文件列表: /path/file1.json /path/file2.json /path/fileN.json 是否有一个(简单的)MATLAB命令可以接受filelist.txt并将每个文件作为字符串读取,并将每个字符串存储到单元格数组中 对于标准函数来说,这个问题有点特殊。但是,结合两种功能很容易实现: 首先,您必须打开该文件: fid = fopen('filelist.txt'); 接下来,您可以使用以下命令逐行阅读: line_ex = fgetl(f

filelist.txt包含文件列表:

/path/file1.json
/path/file2.json
/path/fileN.json

是否有一个(简单的)MATLAB命令可以接受filelist.txt并将每个文件作为字符串读取,并将每个字符串存储到单元格数组中

对于标准函数来说,这个问题有点特殊。但是,结合两种功能很容易实现:

首先,您必须打开该文件:

fid = fopen('filelist.txt');
接下来,您可以使用以下命令逐行阅读:

line_ex = fgetl(fid)
此功能包括一个计数器。如果下次调用该函数,它将读取第二行,依此类推。你可以找到更多的信息

整个代码可能如下所示:

%   Open file
fid = fopen('testabc');

numberOfLines = 3;

%   Preallocate cell array
line = cell(numberOfLines, 1);

%   Read one line after the other and save it in a cell array
for i = 1:numberOfLines
    line{i} = fgetl(fid);
end

%   Close file
fclose(fid);

对于标准函数来说,这个问题有点特殊。但是,结合两种功能很容易实现:

首先,您必须打开该文件:

fid = fopen('filelist.txt');
接下来,您可以使用以下命令逐行阅读:

line_ex = fgetl(fid)
此功能包括一个计数器。如果下次调用该函数,它将读取第二行,依此类推。你可以找到更多的信息

整个代码可能如下所示:

%   Open file
fid = fopen('testabc');

numberOfLines = 3;

%   Preallocate cell array
line = cell(numberOfLines, 1);

%   Read one line after the other and save it in a cell array
for i = 1:numberOfLines
    line{i} = fgetl(fid);
end

%   Close file
fclose(fid);

为此,将For循环替换为while循环:

i=0;
while ~feof(fid)
i=i+1
line{1} = fgetl(fid)
end

为此,将For循环替换为while循环:

i=0;
while ~feof(fid)
i=i+1
line{1} = fgetl(fid)
end

只需使用
readtable
,让它完整地阅读每一行

>> tbl = readtable('filelist.txt','ReadVariableNames',false,'Delimiter','\n');
>> tbl.Properties.VariableNames = {'filenames'}
tbl =
  3×1 table
        filenames     
    __________________
    '/path/file1.json'
    '/path/file2.json'
    '/path/fileN.json'
然后访问循环中的元素

for idx = 1:height(tbl)
   this_filename = tbl.filenames{idx};
end

只需使用
readtable
,让它完整地阅读每一行

>> tbl = readtable('filelist.txt','ReadVariableNames',false,'Delimiter','\n');
>> tbl.Properties.VariableNames = {'filenames'}
tbl =
  3×1 table
        filenames     
    __________________
    '/path/file1.json'
    '/path/file2.json'
    '/path/fileN.json'
然后访问循环中的元素

for idx = 1:height(tbl)
   this_filename = tbl.filenames{idx};
end

自定义功能:

function [lineCount] = numlinestextfile(filename)
%numlinestextfile: returns line-count of filename
%   Detailed explanation goes here
if (~ispc) % Tested on OSX
  evalstring = ['wc -l ', filename];   
  % [status, cmdout]= system('wc -l filenameOfInterest.txt');
  [status, cmdout]= system(evalstring);
  if(status~=1)
      scanCell = textscan(cmdout,'%u %s');
      lineCount = scanCell{1}; 
  else
      fprintf(1,'Failed to find line count of %s\n',filenameOfInterest.txt);
      lineCount = -1;
  end
else

if (~ispc)     % For Windows-based systems
    [status, cmdout] = system(['find /c /v "" ', filename]);
    if(status~=1)
        scanCell = textscan(cmdout,'%s %s %u');
        lineCount = scanCell{3};
        disp(['Found ', num2str(lineCount), ' lines in the file']);
    else
        disp('Unable to determine number of lines in the file');
    end
end
end

自定义功能:

function [lineCount] = numlinestextfile(filename)
%numlinestextfile: returns line-count of filename
%   Detailed explanation goes here
if (~ispc) % Tested on OSX
  evalstring = ['wc -l ', filename];   
  % [status, cmdout]= system('wc -l filenameOfInterest.txt');
  [status, cmdout]= system(evalstring);
  if(status~=1)
      scanCell = textscan(cmdout,'%u %s');
      lineCount = scanCell{1}; 
  else
      fprintf(1,'Failed to find line count of %s\n',filenameOfInterest.txt);
      lineCount = -1;
  end
else

if (~ispc)     % For Windows-based systems
    [status, cmdout] = system(['find /c /v "" ', filename]);
    if(status~=1)
        scanCell = textscan(cmdout,'%s %s %u');
        lineCount = scanCell{3};
        disp(['Found ', num2str(lineCount), ' lines in the file']);
    else
        disp('Unable to determine number of lines in the file');
    end
end
end

我很确定MATLAB中有一个简单的命令,可以逐行读取文本文件。你搜索过吗?我很确定MATLAB中有一个简单的命令,可以逐行读取文本文件。你找过吗?太棒了!避免重新发明车轮很好!避免重新发明轮子的好方法