尝试在MATLAB图形中使用ButtonDownFcn时遇到问题。从这个意义上说,文档似乎有些矛盾,因此很难确切地理解我应该做什么。我想要做的是将函数分配给图中的线。当用户单击分配了该“ ButtonDownFcn”回调函数的任何给定行时,我的程序将同时获取该行对象和一个名为“ selectionObj”的变量,然后将该行对象复制到“ selectionObj”(这是专门编写的类),然后返回新版本的“ selectionObj”,以便用户可以继续选择更多行并组成选择集。具体来说,我的问题是正确地将输入和输出传递给此句柄,因为在这种情况下,我对如何使用语法感到不确定。
我将在下面附加一个MWE(最小工作示例),以便您可以重现我遇到的相同问题。我的问题特别是与此行:
set(lineInPlot{i}, 'ButtonDownFcn', @(linehandle)mouseLineSelectMWE(linehandle), 'PickableParts', 'all')
特别是在“ @(linehandle)mouseLineSelectMWE(linehandle)”处
MATLAB文档说,通常,我可以使用以下形式的句柄
hand = @(s) s.^2 + 1;
result = hand(2); % Which produces result = 5.
但是,在这种情况下,我无法在此处指定左手参数。如果您想到的语言是C及其派生语言,通常这意味着您必须将指针传递到函数中。 MATLAB的方法是指定“全局”变量,该变量可以在任何地方被字面上调用,并且指向内存中的同一位置。因此,在MWE中,selectionObj是全局的,在mouseLineSelectMWE中将其作为全局调用。
此外,回到代码段@(linehandle)mouseLineSelectMWE(linehandle)”,我已经看到人们使用@(〜,〜)function,@function(aRandomVariableHere)等这些句柄,诸如此类那么,我不确定如何在此处指定输入和输出的精确度,尤其是当我需要将该精确的行传递到代码中时。
所以,归根结底,我真正需要修复的是(〜,〜)fun()部分,我不太了解它的工作原理。
MWE:
close all % Clear current plots
clear all
global selectionObj
% selectionObj = BCselectionMWE; % Creation
selectionObj = BCselection; % Creation
%% Starting the BC selection figure
fig1 = figure(1);
ax1 = axes;
hold on
% Plotting lines
for i = 1:4
lineInPlot{i} = plot3(...
rand(2,1), ...
rand(2,1), ...
rand(2,1),...
'Color',[.5 .5 .5],...
'LineWidth',1.2...
);
linehandle = lineInPlot{i}; % Perhaps this is unnecessary
set(lineInPlot{i}, 'ButtonDownFcn', @(linehandle)mouseLineSelectMWE(linehandle), 'PickableParts', 'all')
end
% Figure formatting
grid on
view([50 25]) % Initial POV ([45 30] is also good)
daspect([1 1 1]) % Constraints the data aspect ratio. The geometry won't
% be distorted as you pan around.
%% Class
%
% classdef BCselectionMWE
% %BCSELECTION Summary of this class goes here
% % Detailed explanation goes here
%
% properties (SetAccess = private)
% % Every clicked line will be, first, placed in 'kids'
% kids % ...'kids' because all the 'children' lines will be here
% end
%
% methods
% function obj = BCselection
% %BCSELECTION Construct an instance of this class
% % This class creates objects that make easier the process of
% % organising the selected child lines, their created patches,
% % and the different BC groups created
%
% % We want all properties being created as empty cell arrays.
% kids = cell(0,0);
% end
%
% function obj = addLine(obj,lineChildren)
% %ADDLINE This method adds a line to the current selection
%
% % Doing the stuff necessary if this is a line being added to
% % the selection
% % Highlighting in blue the selected line
% lineChildren.Color(3) = 1;
% % Adding the kid to the kindergarten
% obj.kids{n+1} = lineChildren;
% end
% end
% end
%% Function for callbacks
function selectionObj = mouseLineSelectMWE(lineObj,~)
%MOUSELINESELECT Passes the line object clicked to the addline command of
%the BCselection class. Important since the output can be redirected to the
%class, and not the line object itself.
% THE GLOBAL VARIABLE selectionObj MUST EXIST! selectionObj is an instance
% of the BCselection class
global selectionObj
% Adds (or removes) the line
selectionObj = selectionObj.addLine(lineObj);
% selectionObj.addLine(lineObj);
% This is necessary since the output of the class function must still point
% to the selectionObj variable
end
当前错误消息是:
*Error using lineSelectionMWE>@(linehandle)mouseLineSelectMWE(linehandle)
Too many input arguments.
Error while evaluating Line ButtonDownFcn.*