MATLAB Answers

fseek and parallel reading (vector position)

12 views (last 30 days)
MatG
MatG on 15 Sep 2015
Answered: Guillaume on 15 Sep 2015
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 Comments

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 15 Sep 2015
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 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!