Matlab Gui: Dragging lines on plot using mouse

55 visualizaciones (últimos 30 días)
Robert
Robert el 29 de Mayo de 2024
Respondida: Harimurali el 29 de Mayo de 2024
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.

Respuestas (2)

Adam Danz
Adam Danz el 29 de Mayo de 2024
Editada: Adam Danz el 29 de Mayo de 2024
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).
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

Harimurali
Harimurali el 29 de Mayo de 2024
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:
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!

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by