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

57 views (last 30 days)

Show older comments

Robert on 29 May 2024 at 14:34

  • 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 at 17:31

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 at 15:36

  • 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 at 15:37

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 at 17:31

  • 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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Matlab Gui: Dragging lines on plot using mouse (2024)
Top Articles
How We Built r/Place
What Is R Programming? Use Cases and FAQ
Spasa Parish
2016 Hyundai Sonata Refrigerant Capacity
Cremation Services | Mason Funeral Home serving Westfield, New York...
Q102 Weather Desk
What Is a Megapixel: Essential Guide [Megapixels Explained]
8776685260
Calling All Competitors Wow
Ippa 番号
Craigslist 5Th Wheel Campers For Sale
Nashville Tranny
Timeless - Complete Series Rewatch! / BLOGS | SCIFITVSHOWS
Myhr North Memorial
Dr. Nicole Arcy Dvm Married To Husband
Gdp E239 Bts
When Is Hobby Lobby Opening In Olean Ny
Bleach Tybw Part 2 Gogoanime
Tractorhouse Farm Equipment
Fingerfang Rock Conan
Laura Houston Wbap
Watch The Most Popular Video Of Mikayla Campinos Online
Tamilyogi. Vip
Elijah Vue latest: Two Rivers police confirm remains are those of boy missing since February
Redgifs.comn
Madison Legistar
Atlanticbb Message Center
Active Parent Aberdeen Ms
Lox Club Gift Code
Louisiana Physical Therapy Jurisprudence Exam Answers
Shaleback Hollow Location
T&J Agnes Theaters
Educational Outfitters Denver
Hmnu Stocktwits
Xdefiant turn off crossplay ps5 cмотреть на RuClips.ru
City Md Flatbush Junction
Gofish Dating
Craigslist/Lakeland
Bbc Weather In Mallorca
MAXSUN Terminator Z790M D5 ICE Motherboard Review
Avalon Hope Joi
Kpq News Wenatchee Washington
Trap Candy Strain Leafly
Betty Rea Ice Cream
Saratoga Otb Results
Gen 50 Kjv
The Penitent One Unmasked
Alj Disposition Data
Mazda 6 GG/GG1; GY/GY1 2.3 MPS Test : MPSDriver
Unintelligible Message On A Warning Sign Crossword
Rust Belt Revival Auctions
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5893

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.