2017-05-13 129 views
0

我有多个数据文件,有时候我想删除其中不必要的额外数据行,比如说从行8000到行最后一行。我想为所有包含在.txt文件中的数据文件执行此操作。我曾经手动做过,但经常这样做是一项乏味的工作。 我已经合并和修改的发现我在网上的一些代码,但我有两个问题吧:在Matlab中:如何在多行数据文件中删除从行xxx到文件尾部的行范围

  1. 我不知道如何设置的范围内,从而使代码线8000后删除所有行。直到数据结束为止所以我假设要删除的行数是一些很大的数字(1000)。但这不是一个好的解决方案,因为数据文件中的行总数可能超过9000.
  2. 第二件事是我无法复制或移动创建的临时文件,其中包含没有额外行的新数据以取代原始文件:(我发现临时文件夹中的通用唯一标识符(UUID)与“outfile”中的通用唯一标识符(UUID)不同,例如Matlab中的outfile的UUID代码为tp58460076_aaad_4621_b2ac_c7036febc3f0,并且在temp文件夹中是tp30ade3f8_3abc_4e00_a2ef_26d67c5f836e我已经使用在Matlab

两个copyfilemovefile如果某人有一个替代的解决方案或知道什么是与此代码脚麻,请^ h ELP!

close all 
clear 
clc 
% Specify the folder where the files live. 
myFolder = 'C:\Users\Emma\Data'; 
% Check to make sure that folder actually exists. Warn user if it doesn't. 
if ~isdir(myFolder) 
    errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder); 
    uiwait(warndlg(errorMessage)); 
    return; 
end 
% Get a list of all files in the folder with the desired file name pattern. 
filePattern = fullfile(myFolder, '*.txt'); % Change to whatever pattern you need. 
theFiles = dir(filePattern); 

for k = 1 : length(theFiles) 
    baseFileName = theFiles(k).name; 
    fullFileName = fullfile(myFolder, baseFileName); 
    fprintf(1, 'Now reading %s\n', fullFileName); 
%***************************** 
first_line_to_delete = 8003; 
num_lines_to_delete = 1000; 
infilename = fullFileName; 
[pathstr, file, ext] = fileparts(infilename); 
backfile = fullfile(pathstr, [file '.bak']); 
if strcmp(infilename, backfile) 
    error('I refuse to edit a backup file! Nothing has been changed.'); 
end 

outfile = tempname; %a temporary file in TMP directory 
fin = fopen(infilename, 'r'); 
if fin < 0; error('Input file does not exist'); end 
fout = fopen(tempname, 'w'); 
if fout < 0 
    fclose(fin); 
    error('Could not open temporary output file'); 
end 

%read lines before the one to be deleted and write them to output 
for K = 1 : first_line_to_delete - 1; 
    inline = fgets(fin); 
    if ~ischar(inline); break; end; %end of file? 
    fwrite(fout, inline); 
end 
for K = 1 : num_lines_to_delete; 
    if ~ischar(inline); break; end %in case EOF 
    inline = fgets(fin); %and do nothing with it 
end 
%copy all remaining input lines to output file 
while ischar(inline) 
    inline = fgets(fin); 
    if ischar(inline) %not if we hit EOF 
    fwrite(fout, inline); 
    end 
end 
fclose(fin); 
fclose(fout); 

%we did the copying and have a file with the desired 
%result. Now put it in the proper place 
[status,message,messageId] = copyfile(infilename, backfile, 'f'); %Emma mod 
if ~status 
    if strcmp(infilename, backfile) 
    fprintf(2, 'Good thing your programmer is paranoid about people overriding\nsanity checks, because something went wrong and you nearly lost your file!\n'); 
    else 
    delete(backfile); 
    end 
    error('Could not rename file to .bak, file left untouched'); 
else 
    [status,message,messageId] = copyfile(fullfile(outfile, [myFolder, infilename]), 'f'); 
    if ~status 
    error(['Could not rename temp file to original name, original moved to ', backfile]); 
    end 
end 
%************************** 
end 
+0

http://stackoverflow.com/questions/19017994/how-do-i-limit-or-truncate-text-file逐数的线。否则,只需删除'for K = ...'循环和后续的'while ischar ...'循环。 – beaker

+0

@beaker非常感谢你!一行的shell脚本替换了这个冗长的Matlab代码:)幸运的是,我可以访问一些终端! – Emma

回答

0

回答这个问题是下面的命令:

sed -i '8003,$ d' *.txt