Matlab Gui: Dragging lines on plot using mouse (2024)

31 views (last 30 days)

Show older comments

Robert on 29 May 2024

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2123761-matlab-gui-dragging-lines-on-plot-using-mouse

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2123761-matlab-gui-dragging-lines-on-plot-using-mouse

Answered: Harimurali on 29 May 2024

Matlab Gui: Dragging lines on plot using mouse (2)

I am currently coding this application to quantify the negative/ positive peaks of the signal. The vertical lines in red/green denote the search area to look for the peak negativity or peak positivity. I am controlling the position of these lines usig the numeric text fields labeled with the corresponding name. I would like to be able to click the lines on the graph and drag them to a new position with my mouse. What is the best way to go about implementing this? I was thinking of using imline but it is not reccommended. I would appreciate code for a simple example of just a line being moved to then go off of.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

Adam Danz on 29 May 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2123761-matlab-gui-dragging-lines-on-plot-using-mouse#answer_1465011

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2123761-matlab-gui-dragging-lines-on-plot-using-mouse#answer_1465011

Edited: Adam Danz on 29 May 2024

Open in MATLAB Online

If you have the Image Processing Toolbox, you can use multiple ROI lines that are draggable. By assigning a listener to the line, you can define a function that responds either during the drag (MovingROI event) or when the drag is complete (ROIMoved event).

Matlab Gui: Dragging lines on plot using mouse (4)

In this demo, the axes title is updated during the drag.

uifig = uifigure();

ax = uiaxes(uifig);

plot(ax,sin(0:.1:12))

h = images.roi.Line(ax,'Position',[20 20;ylim(ax)]',... % start at x=20

'Label','P1max', ...

'LabelAlpha',.35, ...

'InteractionsAllowed','translate', ...

'MarkerSize',1, ...

'Color',[0 .7 0] ,...

'StripeColor','w');

% Update title when line is moving

addlistener(h, 'MovingROI', @lineMovingFcn)

function lineMovingFcn(obj,event)

% Update title with new x position of the line

ax = ancestor(obj,'axes');

ax.Title.String = sprintf('x = %.3f',event.CurrentPosition(1));

end

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Harimurali on 29 May 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2123761-matlab-gui-dragging-lines-on-plot-using-mouse#answer_1465061

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2123761-matlab-gui-dragging-lines-on-plot-using-mouse#answer_1465061

Open in MATLAB Online

Hi Robert,

You can refer to the following MATLAB answer that provides an example MATLAB code to manually click a plotted line with the mouse and drag it across the plot:

https://www.mathworks.com/matlabcentral/answers/143306-how-to-move-a-plotted-line-vertically-with-mouse-in-a-gui

If this is not to your liking, you can achieve interactive movement of a vertical line across the plot with the help of a "slider" UI component and a simple callback function that adjusts the position of a line on the plot based on the value of the slider.

Here is an example of MATLAB code that does the same thing mentioned above:

% Create the figure and axes

fig = figure;

ax = axes(fig);

% Plot a sample signal

t = 0:0.01:10;

signal = sin(2*pi*t);

plot(ax, t, signal);

hold(ax, 'on');

% Create the slider

slider_wf = uicontrol(fig, 'Style', 'slider', ...

'Min', 0, 'Max', 10, 'Value', 5, ...

'Position', [75 2 440 20], ...

'SliderStep', [0.01, 0.01], ...

'Callback', @slider_Callback);

function slider_Callback(hObject, eventdata, handles)

% Callback function to update the line position

persistent lineHandle

% Create the line if it doesn't exist yet

if isempty(lineHandle) || ~isvalid(lineHandle)

lineHandle = xline(gca, 5, 'r-', 'LineWidth', 1);

end

% Update the line position based on the slider value

slider_value = get(hObject,'Value');

lineHandle.Value = slider_value;

end

The above code is just for your reference, as the "uicontrol" function cannot be used in App Designer.

You can create a "slider" component for your app using the "uislider" function or by dragging and dropping it from the Component Library in App Designer. The MATLAB code given above can be referred to create a callback for the "slider" component in your app.

Refer to the following documentation for information about the "slider" UI component in App Designer: https://www.mathworks.com/help/releases/R2023a/matlab/ref/uislider.html

Hope this helps!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABApp BuildingMigrate GUIDE Apps

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

  • matlab gui
  • draggable lines

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Matlab Gui: Dragging lines on plot using mouse (6)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Matlab Gui: Dragging lines on plot using mouse (2024)
Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 6448

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.