Scan CSV to variables

前端 未结 1 788
天涯浪人
天涯浪人 2021-01-16 02:50

I have a CSV like this (one line):

101, 120, 130

How can I scan these into variables like this:

pt_num = 101
x = 120
y = 13         


        
相关标签:
1条回答
  • 2021-01-16 03:40

    Just use csvread:

    M = csvread('filename.csv');
    pt_num  = M(:,1);
    x = M(:,2);
    y = M(:,3);
    

    You can also use textscan to get each column in a cell array:

    fid = fopen('filename.csv','r');
    C = textscan(fid,'%d, %n, %n');
    fclose(fid);
    

    And there is fscanf, but you will have to reshape the array:

    fid = fopen('filename.csv','r');
    M = fscanf(fid,'%d, %f, %f')
    fclose(fid);
    M = reshape(M,3,[])';
    

    Finally, dlmread, which works just like csvread:

    M = dlmread('filename.csv',',');
    
    0 讨论(0)
提交回复
热议问题