How can I convert a multidimensional array to csv ?
Mostra commenti meno recenti
Hello,
I have netCDF data and I want to translate it to a csv file with different columns for tmax, tmin, lat, lon, and date. What I have tried so far does not work. The netCDF data is from here.
Code I have tried so far:
file = '../topowx_1980.nc'
tmax = ncread(file, 'tmax')
lat = ncread(file, 'lat')
lon = ncread(file, 'lon')
date = ncread(file, 'time')
M=table(tmax,lat,lon,date)
csvwrite('topowx_1980.csv',M)
*** EDIT**
This actually doesn't work because the variables don't have the same size so matlab doesn't want to create a table
*****
Previous:
This does create a csv file, but the data is not formatted in colums only in a line.
I have also tried to transpose my matrix, but it does not work better.
csvwrite('topowx_1980.csv',M.')
Can someone help?
5 Commenti
just advice here: you should ignore all the netcdf parts of the question and title to 'how to write a table to a csv'. as soon as you have M defined, there's no need to refer to netcdf as being the source at all. your question will get more attention and a solution faster that way
or:
https://www.mathworks.com/help/matlab/ref/writetable.html
Clara Toledano
il 28 Gen 2021
dpb
il 28 Gen 2021
What do the following two commands return?
whos M
head M
i don't think it's netcdf related, but it's probably related to 'date' being a non-numeric data type.
compare
whos date % string, probably
whos tmax % single or double probably
Clara Toledano
il 29 Gen 2021
Modificato: Clara Toledano
il 29 Gen 2021
Risposte (1)
dpb
il 28 Gen 2021
csvwrite is deprecated in favor of writematrix for arrays but is not documented for table inputs (at least thru R2019b).
Having put into the table, use writetable instead.
writetable(M,'topowx_1980.csv')
You may/may not want the variable header line, control that with the named parameter 'WriteVariableNames' with value True (1) which is default or False (0) if don't want the header row.
To save the step of converting to table unless want it for other purposes than just writing the data to a file,
writematrix([tmax lat lon date],'topowx_1980.csv')
There is no headerline this way; if you want one will have to introduce it.
7 Commenti
dpb
il 28 Gen 2021
Well, we don't know what the fields are, specifically.
If the date is a date string or a datetiime, then no, writematrix will not work but it'll die in the catenation of the types inside it before it gets that far.
That's why I asked for what the two information commands return to try to see what actual data are.
the netcdf community usually uses dates in ISO standard.
another option OP can pursue is to cast the dates into epoch time
dates = posixtime(datetime(dates))
or similar so that they're just integers. then writematrix will work without problems
Clara Toledano
il 29 Gen 2021
'Converting to integer will remove leading zeros, whcih are signicant for ISO ...'
invoking posixtime will return the number of seconds elapsed since 1970-01-01 00:00:00 UTC. i have no idea why leading zeros in the output posixtime would make a difference for this. the data are probably originally in ISO 8601 (like above) but the output of posixtime is not and i never claimed that it was. but it is an integer (unless fractions of seconds are included, which i have never seen in a governmentally distributed public netcdf). the leading digits are irrelevant as i see it. i do not see what your comment is intendedto communicate but want to make sure we're talking about the same things
Stephen23
il 29 Gen 2021
@jessupj: you are right. I had a brain-fade and thought your "integer" comment referred to the ISO 8601 dates.
Categorie
Scopri di più su NetCDF in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!