write a function that iterates through a text file and reads in each line in a new array in matlab

main.m
function linesArray = readTextFile(filename)
    fid = fopen(filename, 'r');
    linesArray = {};
    if fid == -1
        error('Cannot open file');
    end
    line = fgetl(fid);
    while ischar(line)
        linesArray = [linesArray; line];
        line = fgetl(fid);
    end
    fclose(fid);
end
301 chars
14 lines

related categories

gistlibby LogSnag