function data = readfromline(filepath, lineNr, colNr) % dirname: Directory and file path string, Ex: dirname = (['./folder/file.dat']) % lineNr: Line to read data from in file with header % colNr: Total nr of columns in file %a = dir(dirName); %filepath = [a(i).folder '/' a(i).name]; fid = fopen(filepath,'rt'); data = textscan(fid, '%f ', 'HeaderLines',lineNr-1); fclose(fid); data_onecolumn = data{1}; frames=length(data_onecolumn)/(colNr); % insert number of cols in file data = reshape(data_onecolumn',colNr,frames)'; if isempty(data) fprintf(1,'Error: trying to read a row with text, look at file you are trying to read\n') end end