How to convert time into nearest hour and to do interpolation of NaN values extracted from .csv file?

9 visualizaciones (últimos 30 días)
Hi,
I have a .csv file from which I extracted values of three columns. Now I want to convert time into nearest hour and interpolate NaN values of last column named 'prec'. I tried to round time using 'dateshift'. However, it is not working. I also tried to fill the missing values. But, it is returning negative values which are supposed to be something else. Can anyone help me please? Thanks
function main
fid = fopen('see.csv','r')
c = textscan(fid,'%s %s %s','delimiter',',');
fclose(fid);
date = c{:,1};
time = c{:,2};
prec = str2double(c{:,3});
Rtime = dateshift(time,'start','hour') + minutes(00); % not working
Fprec = fillmissing(prec,'spline',10,'endvalues','nearest'); % returnig negative values which are supposed be something else
end

Respuesta aceptada

Guillaume
Guillaume el 13 de Mzo. de 2020
Here's how I'd import your data.
opts = detectImportOptions('see.csv'); %let matlab figure out the format of the file.
opts = opts.setvaropts(1, 'Type', 'datetime', 'InputFormat', 'uuuu/MM/dd', 'DatetimeFormat', 'dd/MM/uuuu HH:mm:ss'); %however matlab doesn't detect the first input as datetime
opts.VariableNames = {'DateTime', 'Time', 'Something'}; %give the variable names some more useful names. If the csv file had a header matlab would automatically use it to name the variables
something = readtable('see.csv', opts); %import as table. Obviously use a different name for the output.
something.DateTime = something.DateTime + something.Time; %merge date and time into one datetime
something.Time = []; %and get rid of the time column now that it is part of the date
something.DateTime = dateshift(something.DateTime, 'start', 'hour', 'nearest'); %shift to the nearest hour
something.Something = fillmissing(something.Something, 'linear');
With regards to the fillmissing, considering your numbers are all near 0, it's no wonder you get negative numbers when using cubic interpolation. If you don't want that, use a different fill method.
However, are you sure that you really want to round the time to the nearest hour without affecting the values in the 3rd column? Instead you may want to convert the table to a timetable (at the step before the dateshift, then retime the timetable to hourly:
%.. same code as before up to:
something.Time = [];
something = table2timetable(something);
something = retime(something, 'hourly', 'spline'); %or some other interpolation method
  4 comentarios
Preyanka Dey
Preyanka Dey el 17 de Mzo. de 2020
Thanks Guillaume. The second line is not working. Howerver, I created a timetable and your mentioned 'writetimetable' (last line) also worked. Actually, it has been only two months that I am using MATLAB. That's why I am not familiar with most of the tools. Anyway, again thank you very much for your help and patience.
Guillaume
Guillaume el 17 de Mzo. de 2020
What error message do you get (please give the full message).
Also which version of matlab?
I tested the code with your attached file and get no error.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by