csv reading related matter
1 view (last 30 days)
Show older comments
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
Accepted Answer
Star Strider
on 28 Dec 2023
Try this —
Uz = unzip('2009.zip');
file = Uz{1}
% 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
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
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
More Answers (0)
See Also
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!