Read a whole text file into a MATLAB variable at once

后端 未结 5 717
忘了有多久
忘了有多久 2021-02-10 23:46

I would like to read a (fairly big) log file into a MATLAB string cell in one step. I have used the usual:

s={};
fid = fopen(\'test.txt\');
tline = fgetl(fid);
w         


        
5条回答
  •  盖世英雄少女心
    2021-02-11 00:24

    The main reason your first example is slow is that s grows in every iteration. This means recreating a new array, copying the old lines, and adding the new one, which adds unnecessary overhead.

    To speed up things, you can preassign s

    %# preassign s to some large cell array
    s=cell(10000,1);
    sizS = 10000;
    lineCt = 1;
    fid = fopen('test.txt');
    tline = fgetl(fid);
    while ischar(tline)
       s{lineCt} = tline;
       lineCt = lineCt + 1;
       %# grow s if necessary
       if lineCt > sizS
           s = [s;cell(10000,1)];
           sizS = sizS + 10000;
       end
       tline = fgetl(fid);
    end
    %# remove empty entries in s
    s(lineCt:end) = [];
    

    Here's a little example of what preallocation can do for you

    >> tic,for i=1:100000,c{i}=i;end,toc
    Elapsed time is 10.513190 seconds.
    
    >> d = cell(100000,1);
    >> tic,for i=1:100000,d{i}=i;end,toc
    Elapsed time is 0.046177 seconds.
    >> 
    

    EDIT

    As an alternative to fgetl, you could use TEXTSCAN

    fid = fopen('test.txt');
    s = textscan(fid,'%s','Delimiter','\n');
    s = s{1};
    

    This reads the lines of test.txt as string into the cell array s in one go.

提交回复
热议问题