I have created a .csv file with date and value by
fid=fopen([file(1:4) '.csv'],'w');
fprintf(fid,'%s,%s,%s\n', 'Time','u10','v10');
for i =1:length(t_seris)
fprintf(fid,'%s,%8.4f,%8.4f\n',t_seris(i),u10_p_vct(i),v10_p_vct(i));
end
fclose(fid);
the .csv file generated is '2009.csv'. However, in this file, I found that from line #290, the time format is changed. Then when I import this .csv by table= readtable('2009.csv'), the time column information is gone. May I know what is wrong?

5 件のコメント

Chunru
Chunru 2023 年 12 月 28 日
What is the type and value of t_seris? You may want to post your data.
jie hu
jie hu 2023 年 12 月 28 日
編集済み: KSSV 2023 年 12 月 28 日
% original file is in the .zip file, which is a .nc file after unzip. below the code is what i used to extract data from .nc and create the .csv
VelFiles=dir('./20*.nc');
Nf=length(VelFiles);
for n=1:1
file=VelFiles(n).name(1:end);
% load .nc time
timeout=double(ncread(file,'time'));
t_seris=datetime('1900-01-01 00:00') + hours(timeout);
datetime.setDefaultFormats('reset','dd/MM/yyyy HH:mm');
% load .nc wind data
u10=double(ncread(file,'u10')); v10=double(ncread(file,'v10'));
% take the nearest grid (lower_lon & lower_lat) to the point
u10_p = u10(1,2,:); v10_p = v10(1,2,:);
% convert matrix to column vector
u10_p_vct=u10_p(:); v10_p_vct=v10_p(:);
fid=fopen([file(1:4) '.csv'],'w');
fprintf(fid,'%s,%s,%s\n', 'Time','u10','v10');
for i =1:length(t_seris)
fprintf(fid,'%s,%8.4f,%8.4f\n',datestr(t_seris(i)),u10_p_vct(i),v10_p_vct(i));
end
fclose(fid);
end
jie hu
jie hu 2023 年 12 月 28 日
Hi Chunru, I have uploaded the original data file and the code I used.
KSSV
KSSV 2023 年 12 月 28 日
Why to write to csv when already you have data in good and nice nc file? You may consider using readtable to read csv file.
jie hu
jie hu 2023 年 12 月 28 日
Hi KSSV, it is for further other operations on csv

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

 採用された回答

Star Strider
Star Strider 2023 年 12 月 28 日

0 投票

Thje problem is your not taking full advantage of MATLAB table arrays and everything they can do.
Try this —
Uz = unzip('2009.zip');
file = Uz{1}
file = '2009.nc'
% Info = ncinfo(Uz{1});
% Vbls = Info.Variables;
% Vbls.Datatype;
% Vbls.Name;
% Vbls(3).Attributes.Value;
Time = datetime(1900,1,1) + hours(ncread(file,'time'));
Time.Format = 'dd/MM/yyyy HH:mm';
% datetime.setDefaultFormats('reset','dd/MM/yyyy HH:mm');
% load .nc wind data
u10=double(ncread(file,'u10')); v10=double(ncread(file,'v10'));
% take the nearest grid (lower_lon & lower_lat) to the point
u10_p = u10(1,2,:); v10_p = v10(1,2,:);
% convert matrix to column vector
u10_p_vct=u10_p(:); v10_p_vct=v10_p(:);
u10 = u10_p_vct;
v10 = v10_p_vct;
T2009 = table(Time,u10,v10) % Create Table
T2009 = 8760×3 table
Time u10 v10 ________________ _______ _______ 01/01/2009 00:00 -2.4284 -3.566 01/01/2009 01:00 -3.0311 -4.4606 01/01/2009 02:00 -3.8908 -4.6728 01/01/2009 03:00 -4.0842 -4.1387 01/01/2009 04:00 -4.0012 -4.0258 01/01/2009 05:00 -3.707 -3.966 01/01/2009 06:00 -3.985 -4.0958 01/01/2009 07:00 -4.2243 -3.9444 01/01/2009 08:00 -4.3457 -3.6828 01/01/2009 09:00 -4.5298 -3.3928 01/01/2009 10:00 -4.8411 -2.5877 01/01/2009 11:00 -4.6303 -3.6123 01/01/2009 12:00 -4.0321 -4.0495 01/01/2009 13:00 -2.7064 -3.5143 01/01/2009 14:00 -2.1252 -3.1412 01/01/2009 15:00 -1.8353 -3.1889
writetable(T2009, '2009.csv') % Write Table To File
T2009_2 = readtable('2009.csv'); % Check Result
T2009_2.Time.Format = 'dd/MM/yyyy HH:mm' % Regenerate Desired Time Format
T2009_2 = 8760×3 table
Time u10 v10 ________________ _______ _______ 01/01/2009 00:00 -2.4284 -3.566 01/01/2009 01:00 -3.0311 -4.4606 01/01/2009 02:00 -3.8908 -4.6728 01/01/2009 03:00 -4.0842 -4.1387 01/01/2009 04:00 -4.0012 -4.0258 01/01/2009 05:00 -3.707 -3.966 01/01/2009 06:00 -3.985 -4.0958 01/01/2009 07:00 -4.2243 -3.9444 01/01/2009 08:00 -4.3457 -3.6828 01/01/2009 09:00 -4.5298 -3.3928 01/01/2009 10:00 -4.8411 -2.5877 01/01/2009 11:00 -4.6303 -3.6123 01/01/2009 12:00 -4.0321 -4.0495 01/01/2009 13:00 -2.7064 -3.5143 01/01/2009 14:00 -2.1252 -3.1412 01/01/2009 15:00 -1.8353 -3.1889
I changed your code slightly to make it a bit easier for me to work with. Change it back if you like, however please keep the table arrays! They make this straightforward.
.

2 件のコメント

jie hu
jie hu 2023 年 12 月 28 日
thanks very much!
Star Strider
Star Strider 2023 年 12 月 28 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2023 年 12 月 28 日

コメント済み:

2023 年 12 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by