【发布时间】:2017-10-12 18:52:06
【问题描述】:
我有多个数据文件,有时我想删除其中不必要的额外数据行,例如从第 8000 行到最后一行。我想对 .txt 文件中包含的所有数据文件执行此操作。我以前是手动完成的,但经常这样做是一项乏味的工作。 我已经合并并修改了一些我在网上找到的代码,但我有两个问题:
- 我不知道如何设置范围,这样代码将删除第8000行之后的所有行。直到数据结束因此我假设要删除的行数是一个很大的数字(1000) .但这不是一个好的解决方案,因为数据文件中的总行数可能超过 9000。
- 第二件事是我无法复制或移动创建的临时文件,其中包含新数据而没有额外的行来替换原始文件:(我发现临时文件夹中的通用唯一标识符 (UUID) 是与“
outfile”中的不一样,例如 Matlab 中outfile的 UUID 代码是tp58460076_aaad_4621_b2ac_c7036febc3f0而在临时文件夹中是tp30ade3f8_3abc_4e00_a2ef_26d67c5f836e我在 Matlab 中同时使用了copyfile和movefile
如果有人有替代解决方案或知道此代码出了什么问题,请帮忙!
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
【问题讨论】:
-
stackoverflow.com/questions/19017994/…。否则,只需完全删除
for K = ...循环和随后的while ischar...循环。 -
@beaker 非常感谢!一行的 shell 脚本取代了这个冗长的 Matlab 代码 :) 幸运的是我可以访问一些终端!
标签: matlab file text-files delete-row temp