MATLAB - 删除二进制文件的元素而不加载整个文件
这可能是一个愚蠢的问题,但 Google 和 MATLAB 文档让我失望了。我有一个相当大的二进制文件(> 10 GB),我需要打开并删除最后四千万字节左右。有没有办法做到这一点,而无需将整个文件分块读取到内存中并将其打印到新文件中?生成该文件花了 6 个小时,所以我一想到要重新阅读整个内容就感到畏缩。
编辑:
该文件的大小为 14,440,000,000 字节。我需要把它削减到 14,400,000,000。
This may be a stupid question, but Google and MATLAB documentation have failed me. I have a rather large binary file (>10 GB) that I need to open and delete the last forty million bytes or so. Is there a way to do this without reading the entire file to memory in chunks and printing it out to a new file? It took 6 hours to generate the file, so I'm cringing at the thought of re-reading the whole thing.
EDIT:
The file is 14,440,000,000 bytes in size. I need to chop it to 14,400,000,000.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Matlab 中没有 ftruncate(),但您可以访问 Matlab 中嵌入的 JVM 中的完整 Java 标准库,并且可以使用 java.io.RandomAccessFile 或 Java NIO 类来截断文件。
下面是一个 Matlab 函数,它调用 Java 来删除文件的最后 n 个字节。应具有最小的 I/O 成本。
你也可以把它当作一句台词来做。
There is no ftruncate() in Matlab, but you've got access to the full Java standard library in the JVM embedded in Matlab, and can use java.io.RandomAccessFile or the Java NIO classes to truncate a file.
Here's a Matlab function that calls to Java to lop the last n bytes off a file. Should have minimal I/O cost.
You could also do it as a one-liner.
我发现 Perl 比 MATLAB 执行此操作要快得多。
以下是 Perl Cookbook 中的两个示例:
您可以使用以下命令从 MATLAB 运行 Perl 脚本 : PERL 函数。
I found Perl is much quicker to do this than MATLAB.
Here are two examples from Perl Cookbook:
You can run Perl script from MATLAB with PERL function.
由于您不想将文件读入 MATLAB(可以理解),因此您正在处理系统级命令。 MATLAB 具有使用“system”命令调用系统命令的功能
system
因此,现在您的问题已简化为在操作系统中找到可以为您完成此操作的 shell 命令。或者您可以使用 truncate() (unix -- KennyTM) 或 SetEndOfFile (windows) 编写程序
Since you don't want to read the file into MATLAB (understandably), you are dealing with system level commands. MATLAB has a facility to call system commands using the "system" command
system
So now your problem is reduced to finding the shell command in your OS that will do it for you. Or you can write a program using truncate() (unix -- KennyTM) or SetEndOfFile (windows)
我不知道 MATLAB 是否支持此功能,但请参阅
ftruncate()
和truncate()
。I don't know if MATLAB supports this, but see
ftruncate()
andtruncate()
.