How can I convert a multidimensional array to csv ?
56 ビュー (過去 30 日間)
古いコメントを表示
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?
回答 (1 件)
dpb
2021 年 1 月 28 日
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 件のコメント
jessupj
2021 年 1 月 29 日
編集済み: jessupj
2021 年 1 月 29 日
'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
2021 年 1 月 29 日
@jessupj: you are right. I had a brain-fade and thought your "integer" comment referred to the ISO 8601 dates.
参考
カテゴリ
Help Center および File Exchange で NetCDF についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!