How can I use WindowButtonMotionFcn in an app?

조회 수: 62 (최근 30일)
Matt Brown
Matt Brown 2022년 8월 9일
댓글: Matt Brown 2022년 8월 10일
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.

채택된 답변

Kevin Holly
Kevin Holly 2022년 8월 10일
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
Matt Brown
Matt Brown 2022년 8월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Brown
Matt Brown 2022년 8월 10일
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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by