필터 지우기
필터 지우기

Opening CSV file with numbers and strings

조회 수: 1 (최근 30일)
V
V 2015년 4월 11일
댓글: V 2015년 4월 11일
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,
  댓글 수: 2
dpb
dpb 2015년 4월 11일
Post a (short) section of the file w/ the problem/symptom and show us what you tried, specifically...
V
V 2015년 4월 11일
편집: V 2015년 4월 11일
The code I am using is:
PriceDataLoc = 'C:\123.csv';
PriceData_file = fopen(PriceDataLoc);
Prices1 = textscan(PriceData_file,'%f %f %f %f %f %f','delimiter',' ');
fclose(PriceData_file);
Prices1=[Prices1{:}]
I attach the csv file. A gap in the data is identified with ".". From that point on, the textscan does not read anything else.
Thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 4월 11일
편집: Stephen23 2015년 4월 11일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by