Problem 71. Read a column of numbers and interpolate missing data
Given an input cell array of strings s, pick out the second column and turn it into a row vector of data. Missing data will be indicated by the number 9999. If you encounter missing data, you should perform linear interpolation to the nearest accurate data points (missing data will not occur in the first or last element).
The first row is always descriptive text. So if the input cell array s is
s = { ... 'Day Temp' ' 1 -5' ' 2 19' ' 3 1' ' 4 9999' ' 5 3'};
then the output variable t is the following row vector.
t = [-5 19 1 2 3];
Here's an example of real-world data.
Solution Stats
Problem Comments
-
9 Comments
This is working still would be appreciated for enhancement.
function t = read_and_interp(s)
s=strtrim(s);
row_num=numel(s);
counter=1;
for i=2:row_num
splitted=strsplit(s{i});
temp1=splitted(1);
temp2=splitted(2);
r(counter)=str2double(temp1);
r(counter+1)=str2double(temp2);
counter=counter+2;
end
first_column=r(1:2:end)
second_column=r(2:2:end)
if find(second_column==9999)>=1
first_column(find(second_column==9999))=[];
missed=find(second_column==9999);
second_column(find(second_column==9999))=[];
missed_value=interp1(first_column,second_column,missed)
second_column=r(2:2:end);
second_column(find(second_column==9999))=missed_value;
t=second_column
else
t=second_column
end
Good Problem
beware of splitting.
solution:
https://github.com/AhmedNazir/MatlabCody/blob/master/read_and_interp.m
Interesting!
Solution Comments
Show commentsProblem Recent Solvers2317
Suggested Problems
-
Replace NaNs with the number that appears to its left in the row.
3017 Solvers
-
305 Solvers
-
Project Euler: Problem 8, Find largest product in a large string of numbers
1201 Solvers
-
Project Euler: Problem 9, Pythagorean numbers
1301 Solvers
-
Find my daddy long leg (No 's')
2498 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!