fseek and parallel reading (vector position)
5 次查看(过去 30 天)
显示 更早的评论
fid = fopen('file','rb');
fseek(fid,startposition,'bof');
fread(fid,quantyToRead,'single');
This code will take me to the position in the binary file, for instance a txt file, (specified by fid), and I can read "quantyToRead" elements.
Is there a way, that "startposition" be a vector of positions (as opposed to a scalar), for instance startposition = [1000 40000 80000], and for each position I read "quantyToRead" numvbers. I would like to avoid a for loop to sequentially process and instead do reading all at once. The reason is I have about 10,000 positions to read and and a for loop will take a long time.
0 个评论
回答(1 个)
Guillaume
2015-9-15
file i/o is essentially a sequential operation unless you have a specially designed file system / hardware architecture. Base matlab certainly does not have any concept of parallel i/o and I'm not sure the parallel processing toolbox has either.
My advice would be to read the whole file and then slice the data as appropriate:
fid = fopen('file', 'rb');
alldata = fread(fid, Inf, 'single')
startpositions = [1000, 40000, 80000];
sliceddata = arrayfun(@(sp) alldata(sp:sp+quantyToRead), startpositions, 'UniformOutput', false);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!