read csv data with numbers, characters and text

15 visualizzazioni (ultimi 30 giorni)
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 Commenti
Simon Lind
Simon Lind il 22 Mag 2023
detectImportOptions is a very good tip
Image Analyst
Image Analyst il 22 Mag 2023
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.

Accedi per commentare.

Risposta accettata

Cris LaPierre
Cris LaPierre il 23 Mag 2023
Modificato: Cris LaPierre il 23 Mag 2023
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 Commenti
Simon Lind
Simon Lind il 23 Mag 2023
Spostato: Cris LaPierre il 23 Mag 2023
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));

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Tag

Prodotti


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by