如何使MATLAB圖互動?
我試圖建立一個簡單的介面繪製二次拉格朗日多項式.為此,您只需要3點(每個自己的x,y,z座標),然後使用二次拉格朗日多項式進行內插.
製作靜態版本很簡單,甚至可以在繪製曲線之前讓使用者輸入3點.但是,使用者也可以將繪圖視窗中的現有點拖動到另一個位置,然後使用此點的新位置自動重新繪製曲線!
所以簡而言之,使用者應該能夠將這些黑點拖到另一個位置.之後(或拖動時),曲線應該更新.
function Interact() % Interactive stuff here figure(); hold on; axis([0 7 0 5]) DrawLagrange([1,1; 3,4; 6,2]) function DrawLagrange(P) plot(P(:,1), P(:,2), 'ko--', 'MarkerSize', 10, 'MarkerFaceColor', 'k') t = 0:.1:2; Lagrange = [.5*t.^2 - 1.5*t + 1; -t.^2 + 2*t; .5*t.^2 - .5*t]; CurveX = P(1,1)*Lagrange(1,:) + P(2,1)*Lagrange(2,:) + P(3,1)*Lagrange(3,:); CurveY = P(1,2)*Lagrange(1,:) + P(2,2)*Lagrange(2,:) + P(3,2)*Lagrange(3,:); plot(CurveX, CurveY);
我想我不得不使用WindowButtonDownFcn,WindowButtonUpFcn和WindowButtonMotionFcn等功能,或者從影象處理工具箱中使用ImPoint.但是怎麼樣
[編輯]
它也應該在3D中工作,因為我想將這個概念推廣到張量產品表面.
好的,我從影象處理工具箱中搜索了有關ImPoint選項的更多資訊,並寫了這個指令碼.
由於ImPoint僅適用於2D設定(我想將其歸結為3D,以便能夠使用曲面而不是曲線),這並不是一個可以接受的答案!但有人可能會從中受益,或者想出如何在3D中做到這一點.
% ------------------------------------------------- % This file needs the Image Processing Toolbox! % ------------------------------------------------- function Interact(Pos) % This part is executed when you run it for the first time. % In that case, the number of input arguments (nargin) == 0. if nargin == 0 close all; clear all; clc; figure(); hold on; axis([0 7 0 5]) % I do not know how to do this without global variables? global P0 P1 P2 % GCA = Get handle for Current Axis P0 = ImPoint(gca,1,1); setString(P0,'P0'); P1 = ImPoint(gca,2,4); setString(P1,'P1'); P2 = ImPoint(gca,6,2); setString(P2,'P2'); % Call subfunction DrawLagrange(P0,P1,P2) % Add callback to each point addNewPositionCallback(P0,@Interact); addNewPositionCallback(P1,@Interact); addNewPositionCallback(P2,@Interact); else % If there _is_ some input argument, it has to be the updated % position of a moved point. global H1 H2 P0 P1 P2 % Display X and Y coordinates of moved point Pos % Important: remove old plots! Otherwise the graph will get messy. delete(H1) delete(H2) DrawLagrange(P0,P1,P2) end function DrawLagrange(P0,P1,P2) P = zeros(3,2); % Get X and Y coordinates for the 3 points. P(1,:) = getPosition(P0); P(2,:) = getPosition(P1); P(3,:) = getPosition(P2); global H1 H2 H1 = plot(P(:,1), P(:,2), 'ko--', 'MarkerSize', 12); t = 0:.1:2; Lagrange = [.5*t.^2 - 1.5*t + 1; -t.^2 + 2*t; .5*t.^2 - .5*t]; CurveX = P(1,1)*Lagrange(1,:) + P(2,1)*Lagrange(2,:) + P(3,1)*Lagrange(3,:); CurveY = P(1,2)*Lagrange(1,:) + P(2,2)*Lagrange(2,:) + P(3,2)*Lagrange(3,:); H2 = plot(CurveX, CurveY);
為了清楚起見,我添加了一些意見.
[編輯]在預覽中,語法高亮不是很好!我應該定義哪個語言被突出顯示在某個地方嗎?
http://stackoverflow.com/questions/9646146/how-to-make-a-matlab-plot-interactive