read csv data with numbers, characters and text

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

thank you. I forgot to mention that I'm tryin on M2016b, which doesn'nt have readtable
Cris LaPierre
Cris LaPierre 2023년 5월 22일
편집: Cris LaPierre 2023년 5월 22일
I suggest posting at least a portion of your file so that we can try out different approaches. Use the paperclip icon.
readtable was added in R2013b. In the R2016b time-frame you would very likely want to use detectImportOptions() first.
detectImportOptions is a very good tip
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
Cris LaPierre 2023년 5월 23일
편집: Cris LaPierre 2023년 5월 23일

0 개 추천

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.
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:
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

There are a lot of empty lines. It doesn't appear the 'EmptyLineRule' does anything in R2016b, but you can delete the emptry rows using this code.
idx = all(ismissing(data),2);
data(idx,:)=[];
Simon Lind
Simon Lind 2023년 5월 23일
이동: Cris LaPierre 2023년 5월 23일
I followed your advices. This was what I finally did and it works
Thank you very much
opts.SelectedVariableNames = {'DATE','LATITUDE','LONGITUDE','PRCP'};
T = readtable([datapath myfile],opts);
t = table2array(T(:,1));
lat = table2array(T(:,2));
lon = table2array(T(:,3));
p = table2array(T(:,4));
lon = str2num(cell2mat(lon));
lat = str2num(cell2mat(lat));
t = datenum(t);
p = str2num(cell2mat(p));
Why all the postprocessing? See how to access data in tables.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2016b

태그

질문:

2023년 5월 22일

댓글:

2023년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by