MATLAB: Opening CSV file with numbers and strings

csv

Hi, I am trying to open a csv file which has several columns of numbers. However, often there is a gap in one of the columns and the number gets replaced by "." .
I am trying to load the file into matlab, but if I use fopen followed by textscan, it stops reading on the first "." .
How can I load the csv file into a matlab matrix?
Thanks in advance,

Best Answer

This code reads the all eight lines of your sample file correctly, including those pesky missing data:
fid = fopen('123.csv','rt');
C = textscan(fid,'%f%f%f%f%f%f','TreatAsEmpty','.');
fclose(fid);
And we can check it in the command window:
>> cell2mat(C)
ans =
56266 1.99e+007 19 0 1 98793
56266 1.99e+007 18.75 -0.013158 1 98793
56266 1.99e+007 18.75 0 1 98793
56266 1.99e+007 18.5 -0.013333 1 98793
56266 1.99e+007 19.125 0.033784 1 98793
56266 1.99e+007 NaN -0.03268 1 98793
56266 1.99e+007 18.125 -0.02027 1 98793
56266 1.99e+007 18.125 0 1 98793
The textscan documentation has other useful options which might be of interest to you.