poster
2019-12-10, 16:49
我很少陷入一个问题。我每天都有大量文件生成,我需要按文件名和日期对其进行排序。我需要这样做,以便我的MATLAB脚本可以读取它们。我目前手动执行此操作,但想知道MATLAB中是否有一种更简便的方法来排序和复制文件。
我的文件名看起来像:
data1_2009_12_12_9.10 data1_2009_12_12_9.20 data1_2009_12_12_9.30 data1_2009_12_12_9.40 data2_2009_12_12_9.10 data2_2009_12_12_9.20 data2_2009_12_12_9.30 data2_2009_12_12_9.40 data3_2009_12_12_9.10 data3_2009_12_12_9.20 data3_2009_12_12_9.30 data3_2009_12_12_9.40 ... 和大量这样的文件。
除了上述问题:
必须有一种将文件缝合在一起的简便方法。我的意思是在文件'data1_2009_12_12_9.10'之后复制文件'data1_2009_12_12_9.20',依此类推,...,这样我在结尾处就剩下了一个巨大的txt文件,名为data1_2009_12_12(或之前的版本)。包含所有缝合在一起的数据。现在我唯一知道的方法是在Matlab中使用单独的dlmread命令打开所有文件,然后xls依次写入(或者手动复制粘贴的更简单的方法)
回答:
在功能成像研究领域,我经常不得不将大量文件分类为特定的顺序进行处理。这是一个示例,该示例说明了如何查找文件,解析某些标识符字符串的文件名,然后按照给定的条件对文件名进行排序...
收集文件...
您首先可以使用DIR (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dir.html)函数从目录中获取所有文件名的列表:
dirData = dir('your_directory'); %# Get directory contents dirData = dirData(~[dirData.isdir]); %# Use only the file data fileNames = {dirData.name}; %# Get file names 使用正则表达式解析文件名...
您的文件名似乎具有以下格式:
'data(an integer)_(a date)_(a time)' 因此我们可以使用REGEXP (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexp.html)解析与上述格式匹配的文件名,并提取以下整数data ,日期的三个值和时间的两个值。因此,用于匹配的表达式将为每个有效文件名捕获6个“令牌” (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#f0-56360) :
expr = '^data(\d+)\_(\d+)\_(\d+)\_(\d+)\_(\d+)\.(\d+)$'; fileData = regexp(fileNames,expr,'tokens'); %# Find tokens index = ~cellfun('isempty',fileData); %# Find index of matches fileData = [fileData{index}]; %# Remove non-matches fileData = vertcat(fileData{:}); %# Format token data fileNames = fileNames(index); %# Remove non-matching file names 根据令牌排序...
您可以将上述字符串标记转换为数字(使用STR2DOUBLE (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/str2double.html)函数),然后将日期和时间值转换为日期数字(使用函数DATENUM (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/datenum.html) ):
nFiles = size(fileData,1); %# Number of files matching format fileData = str2double(fileData); %# Convert from strings to numbers fileData = [fileData zeros(nFiles,1)]; %# Add a zero column (for the seconds) fileData = [fileData(:,1) datenum(fileData(:,2:end))]; %# Format dates 变量fileData现在将是一个nFiles fileData -2数值矩阵。您可以使用函数SORTROWS (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sortrows.html)对这些值进行排序。以下代码将首先按单词data之后的整数排序,然后再按日期数字排序:
[fileData,index] = sortrows(fileData,1:2); %# Sort numeric values fileNames = fileNames(index); %# Apply sort to file names 串联文件...
现在, fileNames变量包含给定目录中与所需文件名格式匹配的所有文件的单元格数组,该数组首先按单词data之后的整数排序,然后按日期排序。如果现在要将所有这些文件连接成一个大文件,则可以尝试使用SYSTEM (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/system.html)函数来调用系统命令来为您执行此操作。如果您使用的是Windows机器,你可以不喜欢我在描述这个回答另一个问题,SO (https://stackoverflow.com/questions/1638960/matlab-how-do-you-insert-a-line-of-text-at-the-beginning-of-a-file/1639082#1639082) ,我告诉你如何使用DOS命令用于 (http://www.computerhope.com/forhlp.htm)对连接文本文件 (http://www.howtogeek.com/howto/keyboard-ninja/keyboard-ninja-concatenate-multiple-text-files-in-windows/) 。您可以尝试以下操作:
inFiles = strcat({'"'},fileNames,{'", '}); %# Add quotes, commas, and spaces inFiles = [inFiles{:}]; %# Create a single string inFiles = inFiles(1:end-2); %# Remove last comma and space outFile = 'total_data.txt'; %# Output file name system(['for %f in (' inFiles ') do type "%f" >> "' outFile '"']); 这应该创建一个文件total_data.txt其中包含来自各个文件的所有数据,这些数据按其名称出现在变量fileNames中的顺序连接在一起。请记住,每个文件可能必须以换行符结尾才能正确连接。
更多&回答... (https://stackoverflow.com/questions/1864235)
我的文件名看起来像:
data1_2009_12_12_9.10 data1_2009_12_12_9.20 data1_2009_12_12_9.30 data1_2009_12_12_9.40 data2_2009_12_12_9.10 data2_2009_12_12_9.20 data2_2009_12_12_9.30 data2_2009_12_12_9.40 data3_2009_12_12_9.10 data3_2009_12_12_9.20 data3_2009_12_12_9.30 data3_2009_12_12_9.40 ... 和大量这样的文件。
除了上述问题:
必须有一种将文件缝合在一起的简便方法。我的意思是在文件'data1_2009_12_12_9.10'之后复制文件'data1_2009_12_12_9.20',依此类推,...,这样我在结尾处就剩下了一个巨大的txt文件,名为data1_2009_12_12(或之前的版本)。包含所有缝合在一起的数据。现在我唯一知道的方法是在Matlab中使用单独的dlmread命令打开所有文件,然后xls依次写入(或者手动复制粘贴的更简单的方法)
回答:
在功能成像研究领域,我经常不得不将大量文件分类为特定的顺序进行处理。这是一个示例,该示例说明了如何查找文件,解析某些标识符字符串的文件名,然后按照给定的条件对文件名进行排序...
收集文件...
您首先可以使用DIR (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dir.html)函数从目录中获取所有文件名的列表:
dirData = dir('your_directory'); %# Get directory contents dirData = dirData(~[dirData.isdir]); %# Use only the file data fileNames = {dirData.name}; %# Get file names 使用正则表达式解析文件名...
您的文件名似乎具有以下格式:
'data(an integer)_(a date)_(a time)' 因此我们可以使用REGEXP (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexp.html)解析与上述格式匹配的文件名,并提取以下整数data ,日期的三个值和时间的两个值。因此,用于匹配的表达式将为每个有效文件名捕获6个“令牌” (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#f0-56360) :
expr = '^data(\d+)\_(\d+)\_(\d+)\_(\d+)\_(\d+)\.(\d+)$'; fileData = regexp(fileNames,expr,'tokens'); %# Find tokens index = ~cellfun('isempty',fileData); %# Find index of matches fileData = [fileData{index}]; %# Remove non-matches fileData = vertcat(fileData{:}); %# Format token data fileNames = fileNames(index); %# Remove non-matching file names 根据令牌排序...
您可以将上述字符串标记转换为数字(使用STR2DOUBLE (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/str2double.html)函数),然后将日期和时间值转换为日期数字(使用函数DATENUM (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/datenum.html) ):
nFiles = size(fileData,1); %# Number of files matching format fileData = str2double(fileData); %# Convert from strings to numbers fileData = [fileData zeros(nFiles,1)]; %# Add a zero column (for the seconds) fileData = [fileData(:,1) datenum(fileData(:,2:end))]; %# Format dates 变量fileData现在将是一个nFiles fileData -2数值矩阵。您可以使用函数SORTROWS (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sortrows.html)对这些值进行排序。以下代码将首先按单词data之后的整数排序,然后再按日期数字排序:
[fileData,index] = sortrows(fileData,1:2); %# Sort numeric values fileNames = fileNames(index); %# Apply sort to file names 串联文件...
现在, fileNames变量包含给定目录中与所需文件名格式匹配的所有文件的单元格数组,该数组首先按单词data之后的整数排序,然后按日期排序。如果现在要将所有这些文件连接成一个大文件,则可以尝试使用SYSTEM (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/system.html)函数来调用系统命令来为您执行此操作。如果您使用的是Windows机器,你可以不喜欢我在描述这个回答另一个问题,SO (https://stackoverflow.com/questions/1638960/matlab-how-do-you-insert-a-line-of-text-at-the-beginning-of-a-file/1639082#1639082) ,我告诉你如何使用DOS命令用于 (http://www.computerhope.com/forhlp.htm)对连接文本文件 (http://www.howtogeek.com/howto/keyboard-ninja/keyboard-ninja-concatenate-multiple-text-files-in-windows/) 。您可以尝试以下操作:
inFiles = strcat({'"'},fileNames,{'", '}); %# Add quotes, commas, and spaces inFiles = [inFiles{:}]; %# Create a single string inFiles = inFiles(1:end-2); %# Remove last comma and space outFile = 'total_data.txt'; %# Output file name system(['for %f in (' inFiles ') do type "%f" >> "' outFile '"']); 这应该创建一个文件total_data.txt其中包含来自各个文件的所有数据,这些数据按其名称出现在变量fileNames中的顺序连接在一起。请记住,每个文件可能必须以换行符结尾才能正确连接。
更多&回答... (https://stackoverflow.com/questions/1864235)