How to use interp1 in my situation

2 views (last 30 days)
hmtrondheim Tróndhiem
hmtrondheim Tróndhiem on 4 Jun 2014
Commented: dpb on 5 Jun 2014
Hi
I've a .dat file. I'm supposed to make a function that reads the file, extracts 2/3 parameters and interpolate them. So far I've done this:
function [ output1 , output2 ] = functionname( filename , time )
%Define my delimiter
delimiter = '\t';
% %s are columns included, %*s are columns excluded.
formatSpec = '%s%s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s%[^\n\r]';
%Open the file
fid = fopen(filename,'r');
%Read columns from file using textscan.
dataArray = textscan(fid,formatSpec,'Delimiter',delimiter,'HeaderLines',1,'ReturnOnError',false);
%close the file
fclose(fid);
%Convert data from strings to numbers.
res = [datenum(dataArray{1},'yyyy-MM-dd HH:mm:ss'),str2double(dataArray{2}),str2double(dataArray{3})];
%MOOORE
end
Want I've done is: Opened file, saved 3 parameters in matrix called res, and closed file. Now I need to interpolate. The first column in res is original time of measurements. Measurements are taken every 10 minutes. My input 'time' is every hour. So I've to interpolate from the original measurements time to my 'time'. (In my assignment it's recommended to use interp1)
Measurements were taken over a year, so the matrix res has 50000+ lines. Sometimes measurements weren't taken, and are insted called NaN, I need to do something so this doesn't 'cause problems with other functions.
I hope someone can help me. If you need additional info I'll give it to you ASAP! (I use Matlab 2013b)
  3 Comments
hmtrondheim Tróndhiem
hmtrondheim Tróndhiem on 5 Jun 2014
Can you tell me what goes wrong here:
mask = ~isnan(res(:,2));
res(:,2) = interp1(res(mask,1), res(mask,2), time,'nearest');
I get "The grid vectors are not strictly monotonic increasing." Both res(:,1) and time go from 2013-01-01 to 2013-31-12 just with different pace. Shouldn't they be monotonic increasing?
dpb
dpb on 5 Jun 2014
Seems like should be from that description, yes...
I see you took the mask from the 2nd column of the data values only. Not seeing your data, I wonder from your initial description if you might not need to use the logical 'or' of both column 2 and 3?
Also check the returned datenum values...try
datestr(res(1))
and make sure you get a sensible date string back.
Can check for certain on the order if you try
all(diff(res(mask,1))>0)
(the difference between all successive datenums is >0) If that indeed comes back as 0 (false), then there is definitely something going on in the date conversion.

Sign in to comment.

Answers (0)

Categories

Find more on Dates and Time 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!