如何在matlab中删除文件的第一个字节块
How to delete first block of bytes of a file in matlab
我想在 matlab 中删除文件中的第一个字节块(例如:删除文本文件的前 50 个字节)
这在 matlab 中可能吗?如果是这样,如何实现?
您要使用还是不使用将文件加载到内存中?如果您可以在内存中执行此操作,一种可能的方法是使用 fseek
and fread
, skip the first few bytes, read the rest of the data into memory and save that back into a new file using fwrite
.
读入文件
在 Linux / Mac OS 中,有一些有效的方法可以做到这一点,而无需将文件加载到内存中。例如,参见此处:https://unix.stackexchange.com/questions/6852/best-way-to-remove-bytes-from-the-start-of-a-file
但是,如果您在 Windows 中,则无法避免进行字节复制,这最终意味着在内存中执行此操作。根据我在 Windows 中看到的情况,唯一的方法是进行字节复制,其中输入指针从您想要跳过的任意字节开始。
参见此处的示例:, and also here: 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);
我想在 matlab 中删除文件中的第一个字节块(例如:删除文本文件的前 50 个字节)
这在 matlab 中可能吗?如果是这样,如何实现?
您要使用还是不使用将文件加载到内存中?如果您可以在内存中执行此操作,一种可能的方法是使用 fseek
and fread
, skip the first few bytes, read the rest of the data into memory and save that back into a new file using fwrite
.
在 Linux / Mac OS 中,有一些有效的方法可以做到这一点,而无需将文件加载到内存中。例如,参见此处:https://unix.stackexchange.com/questions/6852/best-way-to-remove-bytes-from-the-start-of-a-file
但是,如果您在 Windows 中,则无法避免进行字节复制,这最终意味着在内存中执行此操作。根据我在 Windows 中看到的情况,唯一的方法是进行字节复制,其中输入指针从您想要跳过的任意字节开始。
参见此处的示例:
有了这些帖子,您别无选择,只能进行字节复制。因此,如果你想在 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);