How can I use WindowButtonMotionFcn in an app?

67 visualizaciones (últimos 30 días)
Matt Brown
Matt Brown el 9 de Ag. de 2022
Comentada: Matt Brown el 10 de Ag. de 2022
Greetings!
I am exploring the app designer and attempting to create GUI for a script which I use frequently. The script is not something I wrote, I borrowed it from the FileExchange (PlotDigitizer).
I have managed to create the app layout and add callbacks and code to load the image and calibrate the plot axes. However, the code relies on WindowButtonMotionFcn to track the mouse pointer and for whatever reason, after the image is loaded, anytime the cursor hovers over the plot I get an error that says:
"Undefined function 'Get_Mouse_Location' for input arguments of type 'matlab.ui.Figure'.
Error while evaluating Figure WindowButtonMotionFcn."
I believe the code responsible for this starts at line 110, but I am at a loss as to why, although, I won't be surprised if it's something simple which I overlooked.
% Get mouse location
set(gcf, 'WindowButtonMotionFcn', @Get_Mouse_Location);
m_location = Get_Mouse_Location(app);
There is far too much code to post, so I have attached it instead. If anyone is up for helping me troubleshoot this so I can understand my mistake (and learn from it!), I would appreciate it.
Thanks in advance,
-Matt
PS - It might help if you tried PlotDigitizer first to understand how the script works, although, if you work from the app... Click Load Image first, locate and load the included plot. The errors will start popping up at that point.

Respuesta aceptada

Kevin Holly
Kevin Holly el 10 de Ag. de 2022
Matt,
I cleaned up your code and changed the Get_Mouse_Location callback in line 110 (see attached). I got rid of the global variables as they are not needed.
  1 comentario
Matt Brown
Matt Brown el 10 de Ag. de 2022
Thanks Kevin! This is much more elegant than my clumsy fix above.
I knew the global variables weren't needed and was going to work my way around to cleaning that up, eventually.

Iniciar sesión para comentar.

Más respuestas (1)

Matt Brown
Matt Brown el 10 de Ag. de 2022
After reviewing examples in the help documentation, I realized the issue was that Get_Mouse_Location was defined as a method and not a subfunction of the function that called it. By moving it inside the calling function, the errors resolve and everything seems to be working as expected. However, I am not sure how this will work out when I add the rest of the functions that call Get_Mouse_Location to the code. I feel like I am stumbling in the dark on this a bit, so if anyone wants to offer advice or clarification, I would appreciate it.
function [ ] = Display_Image(app )
% This function shows an image and croped image
global X_d Y
global x_min x_max y_min y_max
global ratio
global m_location
global data
% Draw main image
imagesc([x_min, x_max], [y_min, y_max], flipdim(X_d,1))
set(gca,'ydir','normal');
% Get mouse location
set(gcf, 'WindowButtonMotionFcn', @Get_Mouse_Location);
m_location = Get_Mouse_Location(app);
% Obtain cropped image
[px1, px2, py1, py2] = Get_Crop_Position(app);
Y = X_d(px1:px2, py1:py2,:);
hold on;
x1 = x_max + ratio*(x_min - x_max);
y1 = y_max + ratio*(y_min - y_max);
imagesc([x1, x_max], ...
[y1, y_max], flipdim(Y,1));
% Plot boarderline for cropped image
%plot( (x1 + x_max)/2, (y1 + y_max)/2, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'red')
plot([1, 1] *(x1 + x_max)/2, [y1, y_max] , 'black:')
plot( [x1, x_max] , [1, 1]*(y1 + y_max)/2, 'black:')
plot([x1, x_max], [y1, y1 ], 'black')
plot([x1, x1 ], [y1, y_max], 'black')
hold off
% Temp: Mark data
if(data)
hold on;
plot(data(:,1), data(:,2), 'cyano--', 'linewidth', 1.0)
hold off
end
function [ Location ] = Get_Mouse_Location( app, object, eventdata )
% This function gets the location of the mouse pointer
Location = get(gca, 'CurrentPoint');
end
end
% function [ Location ] = Get_Mouse_Location( app, object, eventdata )
% % This function gets the location of the mouse pointer
% Location = get(gca, 'CurrentPoint');
% end

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by