Clear Filters
Clear Filters

csv reading related matter

1 view (last 30 days)
jie hu
jie hu on 28 Dec 2023
Commented: Star Strider on 28 Dec 2023
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 Comments
KSSV
KSSV on 28 Dec 2023
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 on 28 Dec 2023
Hi KSSV, it is for further other operations on csv

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 28 Dec 2023
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 Comments
jie hu
jie hu on 28 Dec 2023
thanks very much!
Star Strider
Star Strider on 28 Dec 2023
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!