读取文件夹中所有文本文件的顺序与它们在Matlab中显示的顺序相同
我目前有20个文本文件,从file1到file20命名。我正在使用matlab将它们读入读取文件夹中所有文本文件的顺序与它们在Matlab中显示的顺序相同,matlab,Matlab,我目前有20个文本文件,从file1到file20命名。我正在使用matlab将它们读入 filePattern = fullfile(myFolder, '*.txt'); dataFiles = dir(filePattern); for k = 1:length(dataFiles) baseFileName = dataFiles(k).name; fullFileName = fullfile(myFolder, baseFileName); fid = fopen(fullFi
filePattern = fullfile(myFolder, '*.txt');
dataFiles = dir(filePattern);
for k = 1:length(dataFiles)
baseFileName = dataFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fid = fopen(fullFileName, 'r');
line = fgetl( fid );
while ischar( line )
tks = regexp( line, '\[([^,]+),([^\]]+)\]([^\[]+)\[([^\]]+)\]([^\[]+)', 'tokens' );
for ii = 1:numel(tks)
j=j+1;
mat( j ,: ) = str2double( tks{ii} );
end
line = fgetl( fid );
end
fclose( fid );
end
这是完美的工作,但我需要保持相同的顺序,文本文件出现在文件夹中。将文件1下一个文件2下一个文件3到文件20的数据输入Matlab
但它正在重新排列为file1 file10 file11 file12。。。file2 file20和读取。数据文件是一种结构,文件按字母顺序加载。如何防止这种情况发生?我建议使用(可在Matlab Central上获得)来完成此任务
在空文件夹中运行此操作:
% create sample files
for i = 1:20
filename = sprintf('file%d.txt',i);
fclose(fopen(filename, 'w'));
end
% obtain folder contents
files = dir('*.txt');
%{files.name} % -> list of files might be in alphabetical order (depends on OS)
% sort_nat sorts strings containing digits in a way such that the numerical value
% of the digits is taken into account
[~,order] = sort_nat({files.name});
files = files(order);
% check output is in numerical order
{files.name}
我建议使用(可在Matlab Central上获得)来完成此任务
在空文件夹中运行此操作:
% create sample files
for i = 1:20
filename = sprintf('file%d.txt',i);
fclose(fopen(filename, 'w'));
end
% obtain folder contents
files = dir('*.txt');
%{files.name} % -> list of files might be in alphabetical order (depends on OS)
% sort_nat sorts strings containing digits in a way such that the numerical value
% of the digits is taken into account
[~,order] = sort_nat({files.name});
files = files(order);
% check output is in numerical order
{files.name}