0
複数のデータファイルがありますが、時には8000行から最後の行私は、.txtファイルに含まれているすべてのデータファイルに対してその処理を行いたいと思います。私はそれを手動でやっていましたが、それを頻繁に行うのは面倒な仕事です。 Iを合わせ、オンラインIを発見したいくつかのコードを変更し、私はそれには2つの問題を持っていますMatlab:xxx行から複数のデータファイルの末尾まで行範囲を削除する方法
- 私はコードが行8000の後に、すべての行を削除するように、範囲を設定する方法がわかりません。データの終わりまでです。したがって、削除する行の数はいくらか大きな数(1000)であると仮定しました。しかし、これは良い解決策ではありません。データファイル内の行数の合計が9000を超える可能性があるからです。
- 2番目の点は、余分な行なしで新しいデータを含む作成済みの一時ファイルをコピーまたは移動できないこと(元のファイルを置き換えるために:(テンポラリフォルダ内のユニバーサルユニークな識別子(UUID)が "
outfile
"のものと同じではないことが分かった)Matlabのoutfile
のUUIDコードはtp58460076_aaad_4621_b2ac_c7036febc3f0
であり、誰かが別の解決策を持っているか、このコードで間違って何が起こっているか知っている場合tp30ade3f8_3abc_4e00_a2ef_26d67c5f836e
私はしてください時間、Matlabの に
をcopyfile
とmovefile
の両方を使用していていますエル・P!
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
http://stackoverflow.com/questions/19017994/how-do-i-limit-or-truncate-text-file -by-number-of-lines。さもなければ、 'for K = ...'ループとその後の 'while ischar ...'ループを完全に削除してください。 – beaker
@beakerありがとうございました! 1行のシェルスクリプトがこの長いMatlabコードを置き換えました:)幸いにも私はいくつかの端末にアクセスできます! – Emma