Matlab Gui: Dragging lines on plot using mouse

36 次查看(过去 30 天)
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.

回答(2 个)

Adam Danz
Adam Danz 2024-5-29
编辑:Adam Danz 2024-5-29
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 2024-5-29
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!

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by