You may also consider to convert your file to binary format if your data file is not changing each time you want to load it. Then you'll load it way much faster. Or you may do "transparent binary conversion" like in the function below. Only first time you load the data will be slow. All subsequent will be fast.
function Data = ReadTextFile(FileName,NColumns) MatFileName = sprintf('%s.mat',FileName); % binary file name if exist(MatFileName,'file')==2 % if it exists S = load(MatFileName,'Data'); % load it instead of Data = S.Data; % the original text file return; end fh = fopen(FileName); % if binary file does not exist load data ftom the original text file fh_closer = onCleanup( @() fclose(fh) ); % the file will be closed properly even in case of error Data = fscanf(fh, repmat('%f ',1,NColumns), [NColumns,inf]); Data = Data'; save(MatFileName,'Data'); % and make binary "chache" of the original data for faster subsequent reading end
Do not forget to remove the MAT file when the original data file is changed.
[a,b] = textread(filename, '%f %f')? (assuming your data is numeric).