Clear Filters
Clear Filters

interpolation issues and errors in GUI

2 views (last 30 days)
Jenn
Jenn on 17 Jan 2018
Edited: Greg on 18 Jan 2018
So I have this GUI... a section has a user input area where they put in a series of lat, lon, alt and then specify number of points they want between each value. I have managed to get this interpolation button sorta working in 2 different ways .
Here are over simplified sample lats entered:
5 , 42, 22, 30
and I gave 'interp' a value of 2
Here is the opening of my interpolation function
function interpbtn_Callback(hObject, eventdata, handles)
lla = getappdata(0, 'lla') % this is lat, lon, alt data user entered in prev callback
intrp = str2double(get(handles.interpolateer,'String')) % this is # of points user wants interpolated
lat =lla(:,1)
lon =lla(:,2)
alt =lla(:,3)
First sorta working method is using the interp1 function
L = numel(lat);
x = 1:L;
xp = linspace(1, L, intrp *(L-1) + intrp );
out = interp1(x, lat, xp)
jj = union(lat,out)
The problem here is the 'out' gives me all the interpolated values but leaves out the entered numbers. so I union them. now they are all in order which I don't want. Enter a number higher than 4 for intrp and you get more than requested interp points !!!
out = 5.0000 20.8571 36.7143 36.2857 27.7143 23.1429 26.5714 30.0000
jj =
5.0000
20.8571
22.0000
23.1429
26.5714
27.7143
30.0000
36.2857
36.7143
42.0000
So I try a new method..... I use a for loop and linespace (I initialized handles.latnew =[]; in gui start up)
for i= 1:length (lat)
handles.latnew = [handles.latnew;linspace(lat(i,1), lat((i+1),1), intrp+2)'];
guidata( hObject, handles );
handles.latnew;
setappdata(0,'llaintrpE2',handles.latnew);
end
here is I get almost exactly what I wanted but a double up of values..
5.0000
17.3333
29.6667
42.0000
42.0000 <--- ugh
35.3333
28.6667
22.0000
22.0000 <--- ugh again
24.6667
27.3333
30.0000
I also get a nasty Matlab message every time:
Index exceeds matrix dimensions.
Error in TOOL>interpbtn_Callback (line 727)
handles.latnew = [handles.latnew;linspace(lat(i,1), lat((i+1),1), intrp+2)'];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in TOOL (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)TOOL('interpbtn_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I've been reworking these two methods trying to get them to work for several days now. and this is the closest I've been able to get using both methods. I can't figure out how to tell first method to stop putting in order and why does it give me more interpolated points than what I requested when I request anything more than 4. I can't figure out how to tell second method to leave out duplicates or why I'm getting error message.
help me !
  1 Comment
Greg
Greg on 18 Jan 2018
Edited: Greg on 18 Jan 2018
Read the documentation for union to tell first method to stop putting in order.
It's also giving you exactly what you asked for.
xp = linspace(1, L, intrp *(L-1) + intrp );
% xp ends up being 8 values
% 2*(4-1) + 2 = 8
jj = union(lat,out);
% jj is now 10 values, assuming there are 2 duplicates in lat and out
Finally,
for i= 1:length (lat) % <-- the last iteration of the loop is the maximum index of lat
handles.latnew = [handles.latnew;linspace(lat(i,1), ...
lat((i+1),1), ... % <-- You now add 1 to the max index of lat, it better throw Index exceeds matrix dimensions.
intrp+2)'];

Sign in to comment.

Answers (2)

Greg
Greg on 18 Jan 2018
Edited: Greg on 18 Jan 2018
Edit: I must apologize. I said setdiff below and I meant unique.
You've already found the union function. Scroll to the bottom of the documentation page and check out the other set operations in the See Also section. Specifically, setdiff will help you with method 2, after you fix the other things (sorting and loop index) per my comment above.
  1 Comment
Jenn
Jenn on 18 Jan 2018
Edited: Jenn on 18 Jan 2018
you me rethinking how I wrote the code! I gave up on interp1 although I did go back to union and set it to 'stable' which stopped it from putting them in numerical order but still didn't do what I wanted it to do. So I went back to linespace and your tip about last iteration of loop set me on the right track! Thank you!

Sign in to comment.


Jenn
Jenn on 18 Jan 2018
Edited: Jenn on 18 Jan 2018
For anyone who comes looking here is what worked... I'm sure there is a more efficient way to do this but it works!
function interpbtn_Callback(hObject, eventdata, handles)
% hObject handle to interpbtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
lla = getappdata(0, 'lla');
intrp = str2double(get(handles.interpolateer,'String'));
lat =lla(:,1);
lon =lla(:,2);
alt =lla(:,3);
Alt1=[];
Lon1=[];
Lat1=[];
for i= 1:length (lat)-1 % <--- I added in the -1. this stopped the error messages
Lat1 =[Lat1 linspace(lat(i), lat(i+1), intrp+2)];% <-- combined but still duplicates
Lon1 =[Lon1 linspace(lon(i), lon(i+1), intrp+2)];
Alt1 =[Alt1 linspace(alt(i), alt(i+1), intrp+2)];
Lat = Lat1'; <--- transpose
Lon = Lon1';
Alt = Alt1';
end
%%Look for duplicates and delete
k= [];
for ii = 2:length(Lat)
if Lat(ii)==Lat(ii-1) && Lon(ii)==Lon(ii-1)
k = union(ii,k);
end
end
Lat(k,:) = [];
Lon(k,:) = [];
Alt(k,:) = [];
% save data in GUI
handles.latnew = [handles.latnew;Lat,Lon,Alt];
guidata( hObject, handles );
setappdata(0,'llaintrp',handles.latnew)

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids 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!