How can I convert a multidimensional array to csv ?

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

jessupj
jessupj il 28 Gen 2021
Modificato: jessupj il 28 Gen 2021
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
Hi Jessupj, thank you for your suggestion.. I think my problem is specific to netCDF or at least arrays to csv. If you can help me that would be great!
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
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 !

Accedi per commentare.

Risposte (1)

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

jessupj
jessupj il 28 Gen 2021
Modificato: jessupj il 28 Gen 2021
will writematrix work for strings such as the 'date' fields? that's why i suggest writetable. i imagine matlab complains when you refer to [double double double string]
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
Stephen23
Stephen23 il 29 Gen 2021
Modificato: Stephen23 il 29 Gen 2021
"or similar so that they're just integers. then writematrix will work without problems"
Converting to integer will remove leading zeros, which are significant for ISO 8601 date formats.
Thank you all very much for your help. As you all suggested, I was struggling because I didn't understand the structure of the data. I think I found a solution for my problem, as I posted above.
jessupj
jessupj il 29 Gen 2021
Modificato: jessupj 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
@jessupj: you are right. I had a brain-fade and thought your "integer" comment referred to the ISO 8601 dates.

Accedi per commentare.

Richiesto:

il 28 Gen 2021

Commentato:

il 29 Gen 2021

Community Treasure Hunt

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

Start Hunting!

Translated by