function interpolation(); clear all; clf reset clc global ComputeButton CleanButton ExitButton Exit; global phandle ihandle x s p; x = [0:0.0001:1]'; s = []; p = []; fig = gcf; set(fig,'Unit','normal','Color',[1 1 1],... 'Name','Interpolation','Number','off',... 'MenuBar','none'); set(gca,'Position',[0 0 1.0 1.0],'Box','on',... 'XLim',[0 1],'Ylim',[0 1],'Visible','off'); hold on ihandle = plot(x,x*NaN,'b-'); phandle = plot(NaN,NaN,'r.', 'MarkerSize', 25); drawnow del = 0.1; posx = 0.05; posy = 0.85; ComputeButton = uicontrol('style','push','string','compute', ... 'units','normal','pos',[posx posy 2*del del], ... 'call',['ComputeCallback' ]); CleanButton = uicontrol('style','push','string','clean', ... 'units','normal','pos',[posx+2*del posy 2*del del], ... 'call',['CleanCallback' ]); ExitButton = uicontrol('style','push','string','exit', ... 'units','normal','pos',[posx+4*del posy 2*del del], ... 'call',['ExitCallback' ]); Exit = 0; xy = get(fig,'CurrentPoint'); while (Exit == 0 & ishandle(fig)) xyprev = xy; xy = get(fig,'CurrentPoint'); drawnow pause(0.2); if (xyprev ~= xy) AddPoint(xy); end end % Ajout d'un point function AddPoint(xy) global ComputeButton CleanButton ExitButton Exit; global phandle ihandle x s p; snew = xy(1); pnew = xy(2); disp(['New data : ' num2str(snew) ' ' num2str(pnew) ]); s = [s snew]; p = [p pnew]; set(ComputeButton,'back','default'); set(phandle,'Xdata',s,'Ydata',p); % Calcul de l'interpolation function ComputeCallback() global ComputeButton CleanButton ExitButton Exit; global phandle ihandle x s p; n = length(s); if (n >= 1) set(ihandle,'Ydata',polyval(polyfit(s,p,n-1),x)); set(ComputeButton,'back',[.9 .6 .3]); end % Reset : suppression des données function CleanCallback() global ComputeButton CleanButton ExitButton Exit; global phandle ihandle x s p; s = []; p = []; set(ihandle,'Ydata',x*NaN); set(phandle,'Xdata',NaN,'Ydata',NaN); set(ComputeButton,'back','default'); % Exit : sortie du programme function ExitCallback() global ComputeButton CleanButton ExitButton stop gold; global phandle ihandle x s p; close; Exit = 0;