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....
댓글 수: 0
채택된 답변
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
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
2017년 10월 31일
댓글 수: 3
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 Center 및 File 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!