我想在 MATLAB 中创建一个 txt 文件,例如2GB 大(为简单起见不是 GiB)。它可以包含任意数据。 在 Python(或 C)中可以简单地做到这一点(Python 3.4 代码):
path = r'D:\\file.txt'
f = open(path, 'w')
f.seek(2e9 - 1)
f.write('\x00')
f.close()
这种方法(即寻找过去的文件末尾并写入单个 NUL 值)在 MATLAB R2007b 中不起作用,尽管这种方法在 MATLAB 中有效:
path = 'D:\file.txt';
f = fopen(path, 'w'); % or 'W'
fwrite(f, 0, 'uint8', 2e9 - 1);
fclose(f);
问题是使用 MATLAB 几乎比 Python 慢 17 倍....我没想到执行速度更快,但是 17 倍太多了,1 分钟 <-> 17 分钟是天壤之别。
在 MATLAB 中有更快的方法吗?
规范:
- 操作系统:Windows
- MATLAB:R2007b
- 文件系统:FAT32
编辑:
在 MATLAB 中与在 Python 中相同的方法不起作用,因为 fseek 返回 -1,可能它到达 EOF 并且不喜欢它。这是它的代码:
path = 'D:\file.txt';
f = fopen(path, 'w'); % or 'W'
fseek(f, 2e9 - 1, 'bof') ;
fwrite(f, uint8(0));
fclose(f);
最佳答案
尝试在 MATLAB 中做与在 Python 中相同的事情:
tic;
path = 'D:\file.txt';
f = fopen(path, 'W');
fseek(f, 2e9 - 1, 'eof'); %'Try with 2e8, 2e7...'
fwrite(f, uint8(0));
fclose(f);
toc;
(显然寻找过去的文件 EOF 在 R2007b 中不起作用)
或者,从 MATLAB 执行快速编写的 Python 脚本:
cmd_template = 'python.exe C:\\PathToScript\\new_file.py "%s"'
system(sprintf(cmd_template, 'D:\file.txt'));
当然,“new_file.py”必须接受至少一个输入参数,即新文件名。
关于matlab - 在 MATLAB 中创建充满任意数据的大型 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32225504/