在Matlab中:如何删除多个数据文件中从xxx行到文件结尾的行范围
我有多个数据文件,有时我想删除其中不必要的额外数据行,比如从第8000行到最后一行。我想对所有包含在.txt文件中的数据文件都这样做。我以前是手工做的,但是经常这样做是一件乏味的工作。 我已经合并并修改了一些在网上发现的代码,但我有两个问题:在Matlab中:如何删除多个数据文件中从xxx行到文件结尾的行范围,matlab,file,text-files,delete-row,temp,Matlab,File,Text Files,Delete Row,Temp,我有多个数据文件,有时我想删除其中不必要的额外数据行,比如从第8000行到最后一行。我想对所有包含在.txt文件中的数据文件都这样做。我以前是手工做的,但是经常这样做是一件乏味的工作。 我已经合并并修改了一些在网上发现的代码,但我有两个问题: 我不知道如何设置范围,以便代码将删除第8000行之后的所有行。因此,在数据结束之前,我假设要删除的行数是一个很大的数字(1000)。但这不是一个好的解决方案,因为数据文件中的行总数可能超过9000行 第二件事是,我无法复制或移动创建的临时文件,该文件包含新
outfile
”中的不相同例如,Matlab中outfile
的UUID代码是tp58460076_aaad_4621_b2ac_c7036febc3f0
,在temp文件夹中是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
全部关闭
清楚的
clc
%指定文件所在的文件夹。
myFolder='C:\Users\Emma\Data';
%检查以确保文件夹确实存在。如果不存在,则警告用户。
if~isdir(我的文件夹)
errorMessage=sprintf('错误:以下文件夹不存在:\n%s',myFolder);
uiwait(warndlg(errorMessage));
返回;
结束
%获取文件夹中具有所需文件名模式的所有文件的列表。
filePattern=fullfile(myFolder,*.txt');%更改为您需要的任何模式。
theFiles=dir(filePattern);
对于k=1:长度(文件)
baseFileName=theFiles(k).name;
fullFileName=fullfile(myFolder,baseFileName);
fprintf(1,“正在读取%s\n”,完整文件名);
%*****************************
第一行到删除=8003;
num_line_to_delete=1000;
infilename=fullFileName;
[pathstr,file,ext]=fileparts(infilename);
backfle=fullfile(路径str,[file.bak']);
如果是strcmp(填充、回填)
错误('我拒绝编辑备份文件!未更改任何内容');
结束
outfile=tempname;%TMP目录中的临时文件
fin=fopen(填充名为“r”);
如果fin<0;错误(“输入文件不存在”);结束
fout=fopen(tempname,'w');
如果fout<0
财务总监(财务);
错误('无法打开临时输出文件');
结束
%读取要删除的行之前的行,并将其写入输出
对于K=1:第一行到删除-1;
内联=fgets(fin);
如果~ischar(内联);中断;结束;%文件结束?
fwrite(fout,inline);
结束
对于K=1:要删除的行数;
如果~ischar(内联);中断;在EOF情况下结束%
inline=fgets(fin);%并且不使用它
结束
%将所有剩余的输入行复制到输出文件
而ischar(内联)
内联=fgets(fin);
如果ischar(inline)%not如果我们点击EOF
fwrite(fout,inline);
结束
结束
财务总监(财务);
fclose(fout);
%我们进行了复制,得到了一个包含所需内容的文件
%结果。现在把它放在正确的地方
[status,message,messageId]=复制文件(infilename,backfle,'f');%Emma mod
if~状态
如果是strcmp(填充、回填)
fprintf(2,'幸好您的程序员偏执于人们重写\n一致性检查,因为出了问题,您几乎丢失了文件!\n');
其他的
删除(反写);
结束
错误('无法将文件重命名为.bak,文件保持不变');
其他的
[status,message,messageId]=copyfile(fullfile(outfile,[myFolder,infilename]),'f');
if~状态
错误(['无法将临时文件重命名为原始名称,将原始文件移动到',逆火]);
结束
结束
%**************************
结束
此问题的答案是以下命令:
sed -i '8003,$ d' *.txt
。否则,只需删除K=…循环的
,以及随后的,而ischar…
循环则完全被删除。@bicker非常感谢!一行shell脚本替换了这段冗长的Matlab代码:)幸运的是,我可以访问一些终端!