How to plot graph using ginput?

8 次查看(过去 30 天)
Hi friends i want to plot a graph between two graphic input, i did it but i want to make it as my input should be visible before plotting.
This is my code..
axis([0 100 0 100]);
hold on
nodes = 2;
PauseTime = 0.05;
[x,y] = ginput(nodes);
plot(x,y,'--ys','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','b', 'MarkerSize',4);
please help me....

采纳的回答

OCDER
OCDER 2017-10-30
To do what you want would require using callback functions. See this video here to understand how it works: https://blogs.mathworks.com/videos/2008/05/27/advanced-matlab-capture-mouse-movement/
To summon this function, do:
plotLines(5) %for 5 points, 4 lines
The function is below. You have to modify this based on what you want to accomplish with drawing lines. Play around with the callback functions: startLine, stopLine, dragLine.
function plotLines(nodes)
axis([0 100 0 100]);
set(gca, 'xlim', [0 100], 'ylim', [0 100]);
hold on;
%nodes = 2; %It's now the function input
PauseTime = 0.05; %You can use drawnow instead of pause to refresh plots.
set(gcf, 'WindowButtonDownFcn', @startLine);
set(gcf, 'WindowButtonUpFcn', @stopLine);
function startLine(varargin)
pt = get(gca, 'currentpoint');
if isempty(findobj(gca, 'type', 'line'))
plot(gca, [pt(1,1), pt(1,1)], [pt(1,2), pt(1,2)], '--ys', 'LineWidth',2, 'MarkerEdgeColor','b', 'MarkerFaceColor','b', 'MarkerSize',4);
else
Lx = findobj(gca, 'type', 'line');
Xdata = get(Lx, 'Xdata');
Ydata = get(Lx, 'Ydata');
if length(Xdata) > nodes-1; return; end %Edit this depending on your definition of "node".
Xdata(end+1) = pt(1,1);
Ydata(end+1) = pt(1,2);
set(Lx, 'Xdata', Xdata, 'Ydata', Ydata);
end
set(gcf, 'WindowButtonMotion', @dragLine)
end
function dragLine(varargin)
pt = get(gca, 'currentpoint');
Lx = findobj(gca, 'type', 'line');
Xdata = get(Lx, 'Xdata');
Ydata = get(Lx, 'Ydata');
Xdata(end) = pt(1,1);
Ydata(end) = pt(1,2);
set(Lx, 'Xdata', Xdata, 'Ydata', Ydata);
end
function stopLine(varargin)
set(gcf, 'WindowButtonMotion', '');
end
end
  5 个评论
OCDER
OCDER 2017-10-31
Hm, that's odd. Do you have an older version of Matlab? Try this instead:
clear
close all
clc
axis([0 100 0 100]);
hold on
nodes = 2;
PauseTime = 0.05;
[x,y] = ginput(1);
Lx = plot(x,y,'--ys','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','b', 'MarkerSize',4);
for k = 1:nodes-1
[x,y] = ginput(1);
XData = get(Lx, 'XData');
YData = get(Lx, 'YData');
set(Lx, 'Xdata', [x, XData], 'YData', [y, YData]);
end

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-10-31
Consider using imline()
  3 个评论
Walter Roberson
Walter Roberson 2017-11-1
Instead of using ginput() to get the coordinates, you could imline(), which allows the user to place a draggable resizable line -- so they could effectively select points and move the selection around and see the change in the line, without you having to program in the behaviours yourself.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by