How to plot graph using ginput?

5 ビュー (過去 30 日間)
VALARMATHY K
VALARMATHY K 2017 年 10 月 30 日
コメント済み: VALARMATHY K 2017 年 11 月 6 日
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
VALARMATHY K
VALARMATHY K 2017 年 11 月 1 日
Thank you sir its working

サインインしてコメントする。

その他の回答 (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.
VALARMATHY K
VALARMATHY K 2017 年 11 月 6 日
Thank you sir

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by