Reading text file, matlab

后端 未结 2 1074
刺人心
刺人心 2021-01-25 12:47

I am reading a text file \'mytext.text\' in matlab. Data file looks like:

1   -4436.6910  415.1843    -3019.7497  1,3,4,5,21,23

2   -4366.4541  1353.9975   -308         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-25 13:32

    One possibility is to read the last column as string, then convert it to numbers afterward.

    fid = fopen('file.dat','r');
    C = textscan(fid, '%f %f %f %f %s', ...
        'Delimiter',' ', 'MultipleDelimsAsOne',true, 'CollectOutput',true);
    fclose(fid);
    
    C = [num2cell(C{1}) cellfun(@str2num, C{2}, 'UniformOutput',false)]
    

    The resulting cell-array:

    C = 
        [1]    [-4436.7]    [415.18]    [-3019.7]    [1x6 double]
        [2]    [-4366.5]    [  1354]    [-3085.1]    [1x4 double]
    

    with:

    >> C{1,end}
    ans =
         1     3     4     5    21    23
    >> C{2,end}
    ans =
         1     3     4    23
    

提交回复
热议问题