Code covered by the BSD License  

Highlights from
Load parts of a data file

from Load parts of a data file by Kaare
Function allowing loading of every n'th row and arbitrary columns of named file.

loaded=partialLoading(filename,skipRows,maxrows,cols)
function loaded=partialLoading(filename,skipRows,maxrows,cols)
% function for only loading part of a file, if the file is large.
%
% loaded=partialLoading(filename,skipRows,maxrows,cols)
%
% skiprows: after loading 1 line, skipRows lines will be skipped before
% loading a second line.
% maxrows: maximum number of rows loaded
% cols: which columns to load. if all are needed, input []
%
% The function assumes that the data is a matrix of doubles, and that a single row of the file is
% manageable. Is essentially a wrapper for textscan.

% filename='temp.dat';
% maxrows=2;
% skipRows=3;
% % cols=[1 3 4];
% cols=[];

fid=fopen(filename);
fid_copy=fopen(filename);



%% Generate string to pass to textscan:

string=[];
ncols=numel(cell2mat(textscan(fgetl(fid_copy),'%f')));


for iCol=1:ncols
    if numel(cols)==0 || ismember(iCol,cols)
        string(end+1:end+2)='%f';
    else
        string(end+1:end+3)='%*f';
    end
end

string=[string '%*[\n]'];

%% Read:
if numel(cols)>0
    loaded=zeros(maxrows,numel(cols));
else
    loaded=zeros(maxrows,ncols);
end
i=0;
while ~feof(fid) && i<maxrows
    i=i+1;
    if i==1
        temp=cell2mat(textscan(fid,string,1));
    else
        temp=cell2mat(textscan(fid,string,1,'HeaderLines',skipRows));
    end
    if numel(temp)>0 
        loaded(i,:)=temp;
    end
   if feof(fid)
       i=i-1;
   end
end

loaded(i+1:end,:)=[];

Contact us