Read a whole text file into a MATLAB variable at once

后端 未结 5 699
忘了有多久
忘了有多久 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:06

    Use the fgetl function instead of fread. For more info, go here

    0 讨论(0)
  • 2021-02-11 00:07

    I tend to use urlread for this, e.g.:

    filename = 'test.txt';
    urlname = ['file:///' fullfile(pwd,filename)];
    try
        str = urlread(urlname);
    catch err
        disp(err.message)
    end
    

    The variable str then contains a big block of text of type string (ready for regexp to operate on).

    0 讨论(0)
  • 2021-02-11 00:15

    The following method is based on what Jonas proposed above, which I love very much. How ever, what we get is a cell array s. rather than a single String.

    I found with one more line of codes, we can get a single string variable as below:

    % original codes, thanks to Jonas
    
    fid = fopen('test.txt');
    s = textscan(fid,'%s','Delimiter','\n');
    s = s{1};
    
    % the additional one line to turn s to a string
    s = cell2mat(reshape(s, 1, []));
    

    I found it useful to prepare text for jsondecode(text). :)

    0 讨论(0)
  • 2021-02-11 00:21

    s = regexp(fileread('test.txt'), '(\r\n|\n|\r)', 'split');

    The seashells example in Matlab's regexp documentation is directly on-point.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题