필터 지우기
필터 지우기

csv reading related matter

조회 수: 1 (최근 30일)
jie hu
jie hu 2023년 12월 28일
댓글: Star Strider 2023년 12월 28일
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
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일
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개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by