Importing text files with comments in MATLAB

前端 未结 3 1522
清酒与你
清酒与你 2020-12-30 16:17

Is there any character or character combination that MATLAB interprets as comments, when importing data from text files? Being that when it detects it at the beginning of a

3条回答
  •  囚心锁ツ
    2020-12-30 17:13

    Actually, your data is not consistent, as you must have the same number of column for each line.

    1)

    Apart from that, using '%' as comments will be correctly recognized by importdata:

    file.dat

    %12 31
    12 32
    32 22
    %abc
    13 33
    31 33
    %lffffdd
    77 7
    66 6
    %33 33
    12 31
    31 23
    

    matlab

    data = importdata('file.dat')
    

    2)

    Otherwise use textscan to specify arbitrary comment symbols:

    file2.dat

    //12 31
    12 32
    32 22
    //abc
    13 33
    31 33
    //lffffdd
    77 7
    66 6
    //33 33
    12 31
    31 23
    

    matlab

    fid = fopen('file2.dat');
    data = textscan(fid, '%f %f', 'CommentStyle','//', 'CollectOutput',true);
    data = cell2mat(data);
    fclose(fid);
    

提交回复
热议问题