Read a whole text file into a MATLAB variable at once

后端 未结 5 720
忘了有多久
忘了有多久 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: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). :)

提交回复
热议问题