How to plot graph using ginput?

12 visualizzazioni (ultimi 30 giorni)
VALARMATHY K
VALARMATHY K il 30 Ott 2017
Commentato: VALARMATHY K il 6 Nov 2017
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....

Risposta accettata

OCDER
OCDER il 30 Ott 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 Commenti
OCDER
OCDER il 31 Ott 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
VALARMATHY K
VALARMATHY K il 1 Nov 2017
Thank you sir its working

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 31 Ott 2017
Consider using imline()
  3 Commenti
Walter Roberson
Walter Roberson il 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.
VALARMATHY K
VALARMATHY K il 6 Nov 2017
Thank you sir

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Properties in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by