How can I convert a multidimensional array to csv ?

56 ビュー (過去 30 日間)
Clara Toledano
Clara Toledano 2021 年 1 月 28 日
コメント済み: Stephen23 2021 年 1 月 29 日
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 件のコメント
jessupj
jessupj 2021 年 1 月 28 日
編集済み: jessupj 2021 年 1 月 28 日
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
Clara Toledano 2021 年 1 月 29 日
編集済み: Clara Toledano 2021 年 1 月 29 日
Hello, thank you so much for your help. This is what the output gives :
whos tmax lon lat date
Name Size Bytes Class Attributes
date 12x1 96 double
lat 3250x1 26000 double
lon 7000x1 56000 double
tmax 7000x3250x12 2184000000 double
After posting I re-ran my post and realized that my problem started earlier, because some of my variables were not of the same dimensions and matlab did not want to create a table.
I understood that the tmax variable contained lat and lon infromation. So what I wanted was actually within my tmax variable. My problem was how to transform this 3-dimensional variable into a 2-D matrix with 3 columns each corresponding to lat, lon, and tmax. Only then could I could subsequently export it to csv. Someone helped me write this code to create a new matrix, which works:
j=1
for k=1:7000
for l=1:3250
if tmax(k,l)>0 | tmax(k,l)<0
data_2(j,1)=lon(k,1);
data_2(j,2)=lat(l,1);
for m=1:12
data_2(j,m+2)=tmax(k,l,m);
end
j=j+1;
end
end
end
csvwrite('test.csv',data_2)
Sorry if my question from the beginning was a bit unclear. I am very new to matlab. Thank you so much for your help and reactivity, it's great !

サインインしてコメントする。

回答 (1 件)

dpb
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
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
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 ExchangeNetCDF についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by