我想在matlab中删除文件中的第一个字节块(例如:删除文本文件的前50个字节)
在matlab中有可能吗?如果是这样,如何实现呢?
答案 0 :(得分:2)
您是否要 或 >>将文件加载到内存中?如果你可以在内存中执行此操作,一种可能的方法是使用fseek
和fread
读入文件,跳过前几个字节,将其余数据读入内存并将其保存回来使用fwrite
的新文件。
在Linux / Mac OS中,有一些有效的方法可以在不必将文件加载到内存中的情况下执行此操作。例如,请参见此处:https://unix.stackexchange.com/questions/6852/best-way-to-remove-bytes-from-the-start-of-a-file
但是,如果您在Windows中,则无法进行字节复制,这最终意味着在内存中执行此操作。从我在Windows中看到的,唯一的方法是进行字节复制,其中输入指针以你要跳过的多个字节开始。
例如,请参阅此处:What is the most efficient way to remove first N bytes from a file on Windows?,此处还有http://blogs.msdn.com/b/oldnewthing/archive/2010/12/01/10097859.aspx
使用这些帖子,您无法做出选择,只能进行字节复制。因此,如果你想在MATLAB中模拟相同的东西,你必须按照我上面所说的做。
由于您在MATLAB中工作,以下是一些示例代码,用于执行上面概述的操作:
fid = fopen('data', 'r'); %// Open up data file
fid2 = fopen('dataout', 'w'); %// File to save - new file with skipped bytes
skip = 50; %// Determine how many bytes you want to skip over
fseek(fid1, skip, 'bof'); %// Skip over bytes - 'bof' means from beginning of file
A = fread(fid1); %// Read the data
fwrite(fid2, A); %// Write data to new file
%// Close the files
fclose(fid);
fclose(fid2);