read csv data with numbers, characters and text
조회 수: 41 (최근 30일)
이전 댓글 표시
Greetings
Does anyone have any idea on how to import numerical data from a csv file of the following format:
STATION DATE LATITUDE LONGITUDE ELEVATION NAME PRCP PRCP_ATTRIBUTES TMAX TMAX_ATTRIBUTES TMIN TMIN_ATTRIBUTES
BC000068026 01/02/32 -18.367 21.85 1000 SHAKAWE, BC 0 ,,Q
I'm attaching a sample file
Matlab2016b
Thank you very much in advance
댓글 수: 6
Image Analyst
2023년 5월 22일
If the format is too custom and varies from file to file, you may have to write your own custom reader where you read a line at a time and parse it appropriately.
채택된 답변
Cris LaPierre
2023년 5월 23일
편집: Cris LaPierre
2023년 5월 23일
Have you tried readtable? What didn't work about it? I get the results I would expect. It even successfully ignored the delimiters inside quotes.
The file has 12 delimited fields, the names of which are given in the first line of your file.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1391464/image.png)
The "SHAKAWE, BC" AND ",,Q" are each considered single variables (NAME and PRCP ATTRIBUTES). When I use readtable in R2016b with no options, I get this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1391469/image.png)
Notice the warning about dates. I suspect it should be dd/MM/yy. Use the options to fix that.
opts = detectImportOptions('sample.csv');
opts = setvaropts(opts,'DATE','InputFormat','dd/MM/yy');
data = readtable('sample.csv',opts)
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!