How to plot graph using ginput?
11 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
OCDER
on 30 Oct 2017
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 Comments
OCDER
on 31 Oct 2017
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
More Answers (1)
Walter Roberson
on 31 Oct 2017
3 Comments
Walter Roberson
on 1 Nov 2017
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.
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!