Fastest way to import CSV files in MATLAB

前端 未结 4 976
小鲜肉
小鲜肉 2020-12-16 22:07

I\'ve written a script that saves its output to a CSV file for later reference, but the second script for importing the data takes an ungainly amount of time to read it back

4条回答
  •  醉梦人生
    2020-12-16 22:48

    It would probably make the data easier to read if you could pad the file with NaN values when your first script creates it:

    Item1,1,2,3,NaN
    Item2,4,5,6,7
    Item3,8,9,NaN,NaN
    

    or you could even just print empty fields:

    Item1,1,2,3,
    Item2,4,5,6,7
    Item3,8,9,,
    

    Of course, in order to pad properly you would need to know what the maximum number of values across all the items is before hand. With either format above, you could then use one of the standard file reading functions, like TEXTSCAN for example:

    >> fid = fopen('uneven_data.txt','rt');
    >> C = textscan(fid,'%s %f %f %f %f','Delimiter',',','CollectOutput',1);
    >> fclose(fid);
    >> C{1}
    
    ans = 
    
        'Item1'
        'Item2'
        'Item3'
    
    >> C{2}
    
    ans =
    
         1     2     3   NaN  %# TEXTSCAN sets empty fields to NaN anyway
         4     5     6     7
         8     9   NaN   NaN
    

提交回复
热议问题