内容简介:http://stackoverflow.com/questions/9646146/how-to-make-a-matlab-plot-interactive
我试图创建一个简单的界面绘制二次拉格朗日多项式.为此,您只需要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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Smashing Book
Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90
The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!