How can I convert a multidimensional array to csv ?
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
Antworten (1)
dpb
am 28 Jan. 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 Kommentare
jessupj
am 29 Jan. 2021
Bearbeitet: jessupj
am 29 Jan. 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
am 29 Jan. 2021
@jessupj: you are right. I had a brain-fade and thought your "integer" comment referred to the ISO 8601 dates.
Siehe auch
Kategorien
Mehr zu NetCDF finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!