Scan CSV for Variables

I have a CSV like this (single line):

101, 120, 130 

How can I scan them into the following variables:

 pt_num = 101 x = 120 y = 130 
+1
source share
1 answer

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 an array of cells:

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

And there is fscanf , but you have to resize 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',','); 
+2
source

Source: https://habr.com/ru/post/1382183/


All Articles