using textscan with csv - seemly basic but not working at all
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all,
This is embarrassingly simple but I've had no luck in succeeding in importing my basic csv file using textscan. I've tried several variations of the conversion but nothing is working.
Here's the first few rows of the file:
month date year uavg vavg n mavg davg usd vsd msd dsd
6 1 2006 0.4464 -9.5247 6803 15.64 189.24 9.99 11.88 9.35 65.43
7 1 2006 3.1004 -3.0014 52332 15.3 175.55 12.72 11.54 8.92 89.82
8 1 2006 3.1502 -14.3924 49746 24.72 134.94 17.31 17.34 14.38 65.69
9 1 2006 -4.1448 -8.1912 46222 18.54 182.83 13.84 14.66 12.12 74.05
My attempts have all followed the examples and other codes I've written. There's this one:
fid = fopen('Baja_Border_J1_prep.csv');
data = textscan(fid,'%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32', 'Headerlines', 1);
fclose(fid);
And this one:
data = textscan(fid,'%d %d %d %d %d %d %d %d %d %d %d %d', 'Headerlines', 1);
All I get as output is a cell array that is empty but no error messages that something is wrong.
Any guidance here would be great and much appreciated. I'm really perplexed that it's not working!
Thank you.
댓글 수: 0
채택된 답변
Walter Roberson
2017년 7월 13일
편집: Walter Roberson
2017년 7월 13일
You forgot the 'Delimiter', ',' option.
Note: do not use %d format for fields that are floating point numbers. You could safely use %d for the year field.
I seldom use %f32, which would be for reading single precision. It is justified sometimes, but it does tend to lead to longer programs to mix singles and doubles in the proper manner. Unless I am short on memory I usually just use %f
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!