Search code examples
matlabmatlab-table

What is the difference between readall and read+hasdata?


I found that the functions of readall and read+hasdata seem to be exactly the same. read+hasdata is a loop body, is it less efficient? So in any case you should avoid using read+hasdata? Why does matlab also provide the hasdata function? In what scenario is it more meaningful to use read+hasdata?

hasdata

ds = datastore('mapredout.mat');
while hasdata(ds)
   T = read(ds);
end

readall

ds = datastore('mapredout.mat');
readall(ds)

Solution

  • In this example, there's not really any appreciable difference. However, consider the case where your datastore refers to thousands of data files which contain many gigabytes of data. In that case, it is not practicable to call readall, so you instead proceed by calling read to get chunks of data that you can fit in memory.

    This page shows an example of reading from a large text file in chunks to calculate aggregate quantities without ever having the entire data set in memory at one time.