I think firstly i must make a filter for these interpolations then run these filters to third column of each text file...
1d interpolation from multiple text files
5 次查看(过去 30 天)
显示 更早的评论
I want to apply these two interpolations for missing temperature data in text files
- nearest neighbor interpolation
- bilinear interpolation
My text files are named with Output_00 to Output_23 in a folder. Each text file contains three columns
- First column is latitude
- Second column is longitude
- Third column consist on air temperature corresponding to that latitude and longitude.
In temperature column there are -9999.000 values which representing missing or NaN values.
I want to interpolate these values with above mentioned techniques in such a way that
- Each interpolation technique interpolate temperature(3rd column) in text file and save this interpolated text file with inter_NNH(means this text file has been interpolated with nearest neighbor) and inter_BILIN(means this text file has been interpolated with bi-linear interpolations).
- After that, there will be 72 text files remains in my folder. (24 for uninterpreted and 48 of interpolated.)
I able to attached ten text files(due to size limit) with this question. If any more requirement need tell me first.
Thanks in advance for this kind effort.
6 个评论
Muhammad Usman Saleem
2016-1-31
I able to make this code(It is according to my way to making code, may require improvements, suggestions) %Load each text file and store them in a variable P(index same as text file name)
%Load each text file and store them in a variable P(index same as text file name)
P0=load('Output_00.txt');, P1=load('Output_01.txt');
P2=load('Output_02.txt');,P3=load('Output_03.txt');,P4=load('Output_04.txt');,P5=load('Output_05.txt');,P6=load('Output_06.txt');
P7=load('Output_07.txt');,P8=load('Output_08.txt');,P9=load('Output_09.txt');, P10=load('Output_10.txt');,P11=load('Output_11.txt');
P12=load('Output_12.txt');,P13=load('Output_13.txt');
P14=load('Output_14.txt');, P15=load('Output_15.txt');, P16=load('Output_16.txt');, P17=load('Output_17.txt');, P18=load('Output_18.txt');
P19=load('Output_19.txt');,P20=load('Output_20.txt');,P21=load('Output_21.txt');,P22=load('Output_22.txt');,
P23=load('Output_23.txt');,
%As i want to interpolate -9999.000 value of temperature which is in 3rd
%column that why separate 3rd column of each text file and make another vector
A0 = P0(:,3) ;A1 = P1(:,3) ;A2 = P2(:,3) ;A3 = P3(:,3) ;
A4 = P4(:,3) ;A5 = P5(:,3) ;
A6 = P6(:,3) ;A7 = P7(:,3) ;A8 = P8(:,3) ;A9 = P9(:,3) ;
A10 = P10(:,3) ;A11 = P11(:,3) ;A12 = P12(:,3) ;A13 = P13(:,3) ;
A14 = P14(:,3) ;A15 = P15(:,3) ;A16 = P16(:,3) ;A17 = P17(:,3) ;
A18 = P18(:,3) ;A19 = P19(:,3) ;A20 = P20(:,3) ;A21 = P21(:,3) ;
A22 = P22(:,3) ;A23 = P23(:,3) ;
As an example to Output_00.text file
%FIND -9999.000 VALUES AND ASSISNGE
%THEM TO NAN
A0(A0(:)==-9999.000)=nan
% here will be start interpolation.But i stuck what i do now?
= interp1(,v,xq,method)
- Now my mind is that, code check each value of A0 and interpolate only NaN values with my method of interpolation(like, nearestest neighbor, spline etc.). Values other than NaN remains uninterpreted
Any assistance will be highly appreciable..
Regards
I have this code but not know how to use according to my requirement
interp1(,v,xq,method)
- After interpolating these NaN values, my code pass this interpolated column of temperature to its P0 text file( means makes another text file with name int_methodName.text in which NaN or missing values are interpolated?
Stephen23
2016-2-1
You indicate that you want to interpolate using 1D interpolation, yet bilinear interpolation is 2D interpolation method.
Please clarify if you want 1D or 2D interpolation.
Muhammad Usman Saleem
2016-2-2
Now i have tested this code for ONE file of my data.
% Loading my text file
P0=load('Output_00.txt');,
Reading its 3rd column
A0 = P0(:,3) ;
%assigning -9999.000 values as NaN in this column
A0(A0(:)==-9999.000)=nan;
%Prepare a logical array in which 0 indicating NaN values
nanx = isnan(A0);
%Prepare a vector till length of A0
t = 1:numel(A0);
%Here i am doing interpolation.
A0(nanx) = interp1(t(~nanx), A0(~nanx), t(nanx));
I do not know
- What t(nanx) is doing in the last line of my code? What kind of interpolation it is doing?
- This code running fine. But it not interpolating margin NaN values in my text file( mostly are of bottom ones)?
I have also try this but getting more confusion
A0(nanx) = interp1(t(~nanx), A0(~nanx), 'spline');
Error
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Please if any body have more generalize idea to make code,assist me. Thanks for this kind assistances
Muhammad Usman Saleem
2016-2-3
if any body tell me what is last line of code doing i shall be thankful to him
采纳的回答
Stephen23
2016-2-1
编辑:Stephen23
2016-2-3
Although beginners love using numbered variables and think that using numbered variables will solve all of their problems they actually just make writing code more difficult. To process these you will need to either write lots of similar lines by hand, or use dynamic variable names. These are both BAD ways to program! No matter how obvious it seems to beginners, they need to learn to avoid using numbered variables. Here is why:
Beginners need to learn that if they are numbering their variables then this is a de facto index, and that they should actually use a real index into just one variable. Then your task becomes trivial to achieve. The reason that you are stuck now is because you have chosen the slow and amateur way of coding, by using numbered variables, which makes the task very difficult.
Here is some simple code which reads the files into one variable, and then rearranges it for you to run your interpolation on:
% Read the file data:
S = dir('*Output_*.txt');
N = sort({S.name});
P = numel(N);
R = size(load(N{1},'-ascii'),1);
M = zeros(R,3,P);
for k = 1:P
M(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(M(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[~,~,X] = unique(M(:,1,1));
[~,~,Y] = unique(M(:,2,1));
Z = reshape(M(:,3,:),max(Y),max(X),P);
% Detect -9999
idx = Z<-9998;
% Try your interpolation here!
29 个评论
Muhammad Usman Saleem
2016-2-1
This error occur after just running this code
??? [~,~,X] = unique(M(:,1,1));
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
I want to implement interpolation after that, please assist if you can
Usman
Muhammad Usman Saleem
2016-2-1
@Stephen i am using matlab 2007b version. New improvements are working finely.
I want to apply linear,nearest,next,previous,pchip,cubic,v5cubic,spline interpolations techniques and after will check my results. After observing results i shall final some best interpolation technique.
Problem: My problem is that NaN values are not in sequence. These may occur in the start, middle and even at the end of my text file data. That why difficult for me to final at once.
I prepare more in your code but still getting an error
% Read the file data:
S = dir('*Output_*.txt');
N = sort({S.name});
P = numel(N);
R = size(load(N{1},'-ascii'),1);
M = zeros(R,3,P);
for k = 1:P
M(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(M(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[NA,NA,X] = unique(M(:,1,1));
[NA,NA,Y] = unique(M(:,2,1));
Z = reshape(M(:,3,:),max(X),max(Y),P);
% Detect -9999
Z(Z<-9998) = NaN;
% Try your interpolation here!
interp1(Z,'spline');
Error
interp1(Z,'spline');
Warning: NaN found in Y, interpolation at undefined values
will result in undefined values.
> In interp1 at 178
Muhammad Usman Saleem
2016-2-2
give me some generalize form of code in which i just tell it what kind of interpolation you have to do 3rd column of my text files and it will do this interpolation?
Thanks
Stephen23
2016-2-2
编辑:Stephen23
2016-2-2
What is your question? You have supplied some input data (with NaN's) to interp1. How do you expect it to interpolate NaN values? You need to actually think about the problem you are trying to solve here: those NaN values are at the locations that you are trying to interpolate values at, and yet you are trying to supply them as input values. Interpolation algorithms do not interpolate NaN's , only finite numeric values, therefore you need to supply the interpolation tool with finite numeric values.
Hint: you don't want to pass all of the values to interp1, because some of them are NaN. Remember you can use indexing to select the non-NaN elements, and interpolate where the NaN elements are. You could use a loop, and logical indexing.
Muhammad Usman Saleem
2016-2-2
编辑:Muhammad Usman Saleem
2016-2-2
I have already read document related to interp1...
Now using that code which is running fine. I have no idea what interpolat1 is doing here in this code? When i was testing this code over my previous code(which i prepare above) , this was not interpolating NaN values at the margins.. I do not why? Please see my comment on my above answer..
% Read the file data:
S = dir('*Output_*.txt');
N = sort({S.name});
P = numel(N);
R = size(load(N{1},'-ascii'),1);
M = zeros(R,3,P);
for k = 1:P
M(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(M(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[NA,NA,X] = unique(M(:,1,1));
[NA,NA,Y] = unique(M(:,2,1));
Z = reshape(M(:,3,:),max(X),max(Y),P);
% Detect -9999
Z(Z<-9998) = NaN;
>> nanx = isnan(Z);%logical vector for NaN
>> t = 1:numel(Z);%t run through length of z
>> Z(nanx) = interp1(t(~nanx), Z(~nanx), t(nanx));
%z(why nanx is given here?)= interp1(t(calling elements which are not nan?),z(same here), t(nanx= what is doing this????)
Please explain what interpol1 is doing at the last lines? Your code is very tough to understand to me? Tell me what t(nanx) is doing in the above code?
When i try
Z(nanx) = interp1(t(~nanx), Z(~nanx), 'spline');
i got that error why?
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
And at end end please note that it is not my home work, i am prepare this code in order to data processing of research work
Muhammad Usman Saleem
2016-2-3
编辑:Muhammad Usman Saleem
2016-2-3
Ok. @Stephen i got my mistake.
Now only more step remains
(1) Bind this interpolated column with first two columns of its same file
(2) save these file which are 24 in no in text format with name interpol_method.text?
I think for these two step these two more lines will be added in above code. But i do not know where is first two column of each text file. See this code below
fid=fopen('Interpolate.text','w');
fprintf(fid, [ column 1st ' ' second column '\n(3rd interpolated column']);
fprintf(fid, '%f %f \n', [A B]');% NO IDEA WHAT THESE LINES ARE DOING?
fclose(fid);
Muhammad Usman Saleem
2016-2-3
I have seen that vector Z contains 24 text files where in command window when i call Z i got Z as vector. How can i reconnect this interpolated vector with its first and second column? and how i save this in the form of text file?
Stephen23
2016-2-3
编辑:Stephen23
2016-2-3
You need to learn to check your code as you write it. In particular this means checking that the results are what you expect to get. There is no point in writing these data to files until you have checked that the result is sensible. Just because interp1 is giving you an output does not mean that that output is meaningful or correct. Did you check its output? If so, how?
For example when I checked the array (by comparing it to the data files), I found a bug, and had to change this line:
Z = reshape(M(:,3,:),max(Y),max(X),P);
Your interpolation does not take into account the dimensions of the data array. You have a 3D array, and yet you are interpolating it as 1D data, thus placing some data adjacently that are not adjacent in the data array, like this:
>> X = [1,2,3;4,NaN,6;7,8,NaN].'
X =
1 4 7
2 NaN 8
3 6 NaN
>> X(~isnan(X))also
ans =
1
2
3
4
6
7
8
Do you see that the elements 3 and 4 are not close to each other in the data array, and yet get rearranged next to each other. This makes no sense.
This is why I suggested using a loop (because there can be a different number of input data values along each vector in the array).
Before you interpolate you need to answer a very important question: What dimensions do you want to interpolate over? Over the Latitude (1st dimension), Longitude (2nd dimension), the files (3rd dimension), or some combination of these? If you make it clear what dimensions you want to interpolate over than I can show you how.
Muhammad Usman Saleem
2016-2-3
My question is simple
Every text file consist on temperature data over geographic positions(latitude and longitude). First column of my text file consist on latitude and second on longitude with third column is temperature value at this latitude and longitude. There are 324 latitude and longitude point corresponding to these points there are 324 temperature vales in 3rd column. In my data set there NaN values are appear only in 3rd column. I do not want to interpolate with latitude and longitudes. I want only 3rd column of each text get interpolation(but with out any kind of changing in its order). Such that if there is NaN value corresponding to certain Latitude and Longitude then it interpolated from TEMPERATURE value of its above or below(according to my interpolation technique).
I have checked this code it is running fine. My way of checking was to check only one file(3rd dimmension)interpolated by this code with original text file. I have seen that this code only interpolate NaN values and remaining values are remains unchanged.
Please note that, i am still getting confusion about looping and then saving according to my requirements.
Stephen23
2016-2-3
编辑:Stephen23
2016-2-3
"I have checked this code it is running fine" just because code is running does not mean that its output is correct. You are the code's author, you need to understand what it is doing, and you need to check that it is doing it correctly.
Your code will only interpolate the NaN values, this is true. But for NaN's at the end of a sequence of files it will interpolate with beginning of the next sequence. That is why you cannot simply interpolate all values at once, in one long vector, because this mixes the file sequence into one vector and can give meaningless values. Consider this simple example of two independent columns and we want to interpolate/extrapolate along the columns to get rid of the NaN's, using your method:
>> M = [1,2,3,NaN;NaN,NaN,0,0]'
M =
1 NaN
2 NaN
3 0
NaN 0
>> X = isnan(M);
>> Z = 1:numel(M);
>> M(X) = interp1(Z(~X),M(~X),Z(X))
M =
1.00000 1.50000
2.00000 0.75000
3.00000 0.00000
2.25000 0.00000
Ouch! Where did 2.25 come from? Or 1.5, or 0.75? Do these look correct to you? This is totally wrong! In fact our desired result is actually this matrix:
1 0
2 0
3 0
4 0
The reason why it has not interpolated along the columns is because your methods puts all of the (input) Y values into one vector, like this:
>> M(~X)
ans =
1
2
3
0
0
There are the input Y values, and interp1 provides you with some new Y values between the 3 and 0. This does not conceptually make any sense though, because the 3 and 0 are in different columns and have nothing to do with each other (remember, the columns are supposed to be independent).
So your concept is fundamentally flawed, unless you don't mind that the ends can end up with garbage values. Like I wrote in my earlier comment, you will need to use a loop and interpolate each page (3rd dimension) of the data array separately. Like this:
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VX,NA,IX] = unique(mat(:,1,1));
[VY,NA,IY] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),max(IY),max(IX),nmf);
% Detect -9999
idx = out<(-9998);
% Interpolation:
vec = 1:nmf;
for r = 1:size(out,1)
for c = 1:size(out,2)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'linear','extrap');
end
end
end
Note that if the number of NaN's was the same along each page (3rd dimension), then we would not need the loops, and could use interp1's ability to works with arrays. I fixed some other bugs too, so use this code, not the one in my answer.
Muhammad Usman Saleem
2016-2-3
编辑:Muhammad Usman Saleem
2016-2-4
I have checked my code , yes you are right, Its interpolating over bottom and top of text file's NaN based on previous text file columns. Even i change this to spline. You are right @Stephon...
But your code is tremendous difficult for me to understand. I try to understand it by running each commands but not full understand it.
Please remember that there is no link of NaN values with other pages(means next file 3rd column). NaN values are randomly appeared in 3rd column of each text file. Some times these values appear at starting, middle and bottom. Some time these NaN values appear in sequences and randomly. We can not say any thing about these NaN values but these are appeared in 3rd column of each text file. Also there is not any connection of these NaN value with next or previous text file NaN values.
Tell me if i able to explain my question or not?? @Stephon
Thanks you for this mirror and most important correction in my code.
Muhammad Usman Saleem
2016-2-3
totally not understand these lines
Note that if the number of NaN's was the same along each page (3rd dimension), then we would not need the loops, and could use interp1's ability to works with arrays. I fixed some other bugs too, so use this code, not the one in my answer.
Stephen23
2016-2-4
编辑:Stephen23
2016-2-4
Many of your other questions have shown you how to save data to files, including these ones:
I am confident that you can figure out how to do that, based on the information from all of your other questions.
"I try to understand it by running each commands" that is great way to learn how code works. You should also read the documentation.
The second-to-last sentence just points out that iterp1 can also accept array inputs, and that a loop is not required when the Xin and Xout points are the same. This does not apply in your case. This is why we had to use two loops.
Muhammad Usman Saleem
2016-2-4
编辑:Muhammad Usman Saleem
2016-2-4
I have print out of this code and running each line with searching on google for new function.
Loop implementation is creating trouble
for r = 1:size(out,1)
for c = 1:size(out,2)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'linear','extrap');
end
end
end
How many times inner loop run and what check is doing here?
Stephen23
2016-2-4
编辑:Stephen23
2016-2-4
"Loop implementation is creating trouble" What trouble is it creating? Does it run? Do you get a result? Do you get an error message?
"How many times inner loop run" presumably someone with a "BS(Hon's) in space science" and a "M.Phil in Geomatics" can also figure this one out. Why not look at the code and figure it out:
- what the loops are iterating over
- then you will know how many times the inner loop runs.
If you are still confused about loops, then try doing the introductory tutorials:
"what check is doing here" perhaps you are referring to that if statement: it checks if any of the selected values (i.e. along the 3rd dimension, the page) need to be interpolated. If there are none (i.e. no -9999 values), then there is no point in running interp1.
"searching on google for new function" this is very good idea. Then you can learn to read the official MATLAB documentation.
Muhammad Usman Saleem
2016-2-4
编辑:Muhammad Usman Saleem
2016-2-4
My qualifications are not associated with looping! Every one has his own way of learning!!
Your assistance always take appreciation...
I think you should take some tea then come back to this form
Regards
Stephen23
2016-2-4
编辑:Stephen23
2016-2-4
A cup of tea would be nice.... could you send me some?
Here are ways to figure out how many times the inner loop runs:
- add a counter variable, and check its value after the code finishes (a universal solution).
- multiply the number of times the outer and inner loops run. This is easy because you can read the code (hint: look for size(out,...), and note that this is the number of rows and columns of the 3D data array).
- Understand the algorithm: each latitude and longitude is independently interpolated (as you requested), therefore it must iterate for each latitude and longitude...
- which is incidentally the number of lines of data in the data files.
Just out of curiosity, what do you need this value for?
Muhammad Usman Saleem
2016-2-5
编辑:Muhammad Usman Saleem
2016-2-5
I have not time to serve for tea. But as insistence going on I do on one condition:
In order to exporting this interpolated column with their corresponding first two columns i am using that way. But not sure whether i am doing correct or not
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for x=0:23
% Verbose.
fprintf( 'Export for Interpolation. %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
fId = fopen( sprintf( 'Interpolated_%d.txt', x), 'w' ) ;
fprintf( fId, ' \t \t \r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', ????? ) ;
fclose( fId ) ;
end
At the place of ??? how can i rejoin blocks of each text file interpolated 3rd column with corresponding first two columns? I know mat is 323x3x24 double matrix and my interpolated 3rd columns of each are in out matrix. How can i export these as first column is lat, second long and 3rd as interpolated clumn. Now there will be another 34 text files but interpolated.
It was part of my question. With out this, solution will be not completed...................
Stephen23
2016-2-5
编辑:Stephen23
2016-2-5
Try this:
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'linear','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
Note that I used fopen with the 't' option selected, so that you do not need to specify the carriage-return character with every newline.
Muhammad Usman Saleem
2016-2-8
@Stephon check it whether it running fine or not ??
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'cubic','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_cubic%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'next','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_next%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'pchip','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_pchip%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'previous','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_previous%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'v5cubic','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_v5cubic%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'linear','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_linear%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,'nearest','extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_nearest%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
Stephen23
2016-2-8
编辑:Stephen23
2016-2-8
@Muhammad Usman Saleem: don't repeat code like that. The one thing computers are good at is repeatedly doing tasks, so why waste your time doing the computers work. Use a for-loop and loop over a cell array of the interpolation methods (as strings). This will be easier to understand and easier to maintain. Whenever you find yourself copying code like that either:
- Use a loop.
- Write a function.
Basically do this:
metC = {'linear','pchip','...'};
for m = 1:numel(metC)
metS = metC{m} % the method
... my code
out(r,c,idy) = interp1(Xold,Yold,Xnew,metS,'extrap');
...
end
I am sure you can figure out how to put the method in the filenames too (hint: the best method is to use sprintf).
Muhammad Usman Saleem
2016-2-8
Function will be the best idea but i do not want to make it here. I try this
metC = {'linear','cubic','next','pchip','previous','spline','v5cubic','nearest'};
for m = 1:numel(metC)
metS = metC{m} % the method
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,metS,'extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf( 'Interpolated_?????%d.txt', k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
end
In ???? when loop run it create mentioned interpolation text file and store in text like its interpolation technique. like
Interpolated_method(which has implemented on this file)_00.text
Muhammad Usman Saleem
2016-2-8
编辑:Muhammad Usman Saleem
2016-2-8
as in the first line we have cell array in hand. We have to call each variable every time loop run..
I try
str = sprintf( 'Interpolated_metC{m}%d.txt', k-1);
not completing my task
Stephen23
2016-2-8
编辑:Stephen23
2016-2-8
The string (interpolation method) needs to be its own input into sprintf:
str = sprintf('Interpolated_%s_%d.txt', metS, k-1);
Read the sprintf documentation to know how this works: search for %s.
Optional Change: note that the code currently reads the original data files for every interpolation method. To speed up the code we could read the original files just once and then interpolate on the same data multiple times. This works because we do not change the original data points.
It just requires moving that for-loop from the start to here:
... all of the file reading
% Interpolate:
vec = 1:nmf;
for m = 1:numel(metC) % <- insert |for| here
metS = metC{m} % <- and the method here
for r = 1:numel(VR)
... all of the interpolation
Now the files only get read once, and the code will be quicker.
Muhammad Usman Saleem
2016-2-8
metC = {'linear','cubic','next','pchip','previous','spline','v5cubic','nearest'};
% Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);
% Interpolate:
vec = 1:nmf;
for m = 1:numel(metC)
metS = metC{m} % the method
for r = 1:numel(VR)
for c = 1:numel(VC)
idy = squeeze(idx(r,c,:)).';
if any(idy)
Xold = vec(~idy);
Yold = squeeze(out(r,c,~idy)).';
Xnew = vec(idy);
out(r,c,idy) = interp1(Xold,Yold,Xnew,metS,'extrap');
end
end
end
% Write to file:
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
for k = 1:nmf
fprintf('Export Interpolation. %d -> p=%.1f hpa.\n', k-1, pressures(k) );
str = sprintf('Interpolated_%s_%d.txt', metS, k-1);
fid = fopen(str,'wt');
fprintf(fid,' \t \t \n');
tmp = out(:,:,k);
fprintf(fid,'%.3f\t%.3f\t%.3f\n',[mat(:,1:2),tmp(:)].');
fclose(fid);
end
end
Documentations tell me Read series of characters, until find white space
we have used %s and %d for calling string and base 10 number from cell array and display with output format. We have given their values ahead to sprintf as metS, k-1
Please check again my final version of code and tell me as final free from any more bug or modifications.
Thanks for kind learning.
Muhammad Usman Saleem
2016-2-15
编辑:Muhammad Usman Saleem
2016-2-16
@Stephen i want to use interp2 for my interpolation? How can i modify this code in order to do interpolation in 2 dimension?
I unable to get where you edited? code is same as old
Stephen23
2016-2-16
编辑:Stephen23
2016-2-16
This is a slightly more complicated task because the interp2 function only accepts "2-D gridded data in meshgrid format". Because of your missing data you do not have gridded data, but scattered data points.
You could resolve this yourself by browsing the MATLAB documentation. Start at the link for interp2 above. You read the very first line and realize that this function will not work (because you do not have gridded data). So you look at the contents on the left-hand side of the page. You click on "Interpolation", because that seems to be the best place to look. Then you see a page with a list of three subcategories: 1-D Interpolation, Gridded Data Interpolation, and Scattered Data Interpolation. Which of these do you think would be best for interpolating your scattered data? Click on the link... there are several functions, and pages of examples.
Try the examples. Think about your task and how to solve it using those examples.
If you get stuck, then ask a new question (with a link to this one) showing how you modified those examples to fit your task.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation of 2-D Selections in 3-D Grids 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)