I have a CSV file, I want to read this file and do some pre-calculations on each row to see for example that row is useful for me or not and if yes I save it to a new CSV file. can someone give me an example? in more details this is how my data looks like: (string,float,float) the numbers are coordinates.
ABC,51.9358183333333,4.183255
ABC,51.9353866666667,4.1841
ABC,51.9351716666667,4.184565
ABC,51.9343083333333,4.186425
ABC,51.9343083333333,4.186425
ABC,51.9340916666667,4.18688333333333
basically i want to save the rows that have for distances more than 50 or 50 in a new file.the string field should also be copied. thanks
Just read it in to MATLAB in one block
fid = fopen('file.csv');
data=textscan(fid,'%s %f %f','delimiter',',');
fclose(fid);
You can then process it using logical addressing
ind50 = data{2}>=50 ;
ind50 is then an index of the rows where column 2 is greater than 50. So
data{1}(ind50)
will list all the strings for the rows of interest.
Then just use fprintf
to write out your data to the new file
You cannot read text strings with csvread. Here is another solution:
fid1 = fopen('test.csv','r'); %# open csv file for reading
fid2 = fopen('new.csv','w'); %# open new csv file
while ~feof(fid1)
line = fgets(fid1); %# read line by line
A = sscanf(line,'%*[^,],%f,%f'); %# sscanf can read only numeric data :(
if A(2)<4.185 %# test the values
fprintf(fid2,'%s',line); %# write the line to the new file
end
end
fclose(fid1);
fclose(fid2);
If you really want to process your file line by line, a solution might be to use fgetl
:
fopen
fgetl
sscanf
on the character array you just readUnlike the previous answer, this is not very much in the style of Matlab but it might be more efficient on very large files.
Hope this will help.
here is the doc to read a csv : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.html and to write : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvwrite.html
EDIT
An example that works :
file.csv :
1,50,4.1 2,49,4.2 3,30,4.1 4,71,4.9 5,51,4.5 6,61,4.1
the code :
File = csvread('file.csv') [m,n] = size(File) index=1 temp=0 for i = 1:m if (File(i,2)>=50) temp = temp + 1 end end Matrix = zeros(temp, 3) for j = 1:m if (File(j,2)>=50) Matrix(index,1) = File(j,1) Matrix(index,2) = File(j,2) Matrix(index,3) = File(j,3) index = index + 1 end end csvwrite('outputFile.csv',Matrix)
and the output file result :
1,50,4.1 4,71,4.9 5,51,4.5 6,61,4.1
This isn't probably the best solution but it works! We can read the CSV file, control the distance of each row and save it in a new file.
Hope it will help!
Source: Stackoverflow.com