如何从 MATLAB 中的另一个函数文件执行回调函数?

发布于 2024-08-13 03:38:47 字数 315 浏览 2 评论 0原文

我有两个函数:一个函数用按钮创建 UI,另一个函数我想从中执行与按下按钮相同的函数。

当我使用 get(gcf,'children') 深入研究该图时,我找到了按钮,其回调属性如下所示:

ans = 
    [function_handle]
    [              1]
    [              1]
    [1x6 double]

现在,据我所知,第一个数组元素我应该能够执行与从 UI 按下按钮时执行的功能相同的功能,但我该怎么做呢?我尝试了一切,但似乎没有任何效果。

I have two functions: one which creates the UI with buttons, and another one from which I'd like to execute the same function as pressing the button does.

When I dig into the figure with get(gcf,'children') I find the buttons, with a Callback property that looks like this:

ans = 
    [function_handle]
    [              1]
    [              1]
    [1x6 double]

Now, as far as I understand, with the first array element I should be able to execute the same function as is executed when the button is pressed from the UI, but how do I do that? I tried everything, but nothing seems to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2024-08-20 03:38:47

从按钮回调获得的结果来看,回调似乎是通过以下方式创建的(仅作为示例):

hButton = uicontrol(...,'Callback',{@button_callback,1,1,[1:6]});

其中回调函数 button_callback 定义如下:

function button_callback(hObject,eventdata,a,b,c)
  ...
end

请注意,有 < a href="http://www.mathworks.com/help/matlab/creating_guis/write-callbacks-using-the-programmatic-workflow.html#brqow8p" rel="nofollow noreferrer">两个额外参数< /em> 回调函数的输入参数列表:hObject(调用回调的对象的句柄)和eventdata(事件数据结构) )。

如果要使用应传递给函数句柄的 3 个附加参数(11 和 1×6 数组)来调用函数句柄,则需要还可以传递 hObjecteventdata 输入的参数。调用函数的方式如下(使用变量 ans):

ans{1}(hButton,[],ans{2:end});

首先从元胞数组 (ans{1}) 获取函数句柄,然后 像使用任何其他函数一样使用括号调用它。对于hObject,您可以将句柄传递给uicontrol对象(如果不需要,则可以传递一个空值),对于eventdata,您可以只传递一个空值。然后从元胞数组中以 comma- 形式获取附加值分隔列表 (ans{2:end}),每个列表都作为单独的附加参数传递给函数。

From the result you got for the button callback, it appears that the callback has been created in the following way (just for example):

hButton = uicontrol(...,'Callback',{@button_callback,1,1,[1:6]});

where the callback function button_callback is defined as follows:

function button_callback(hObject,eventdata,a,b,c)
  ...
end

Notice that there are two extra arguments in the input argument list for the callback function: hObject (the handle of the object invoking the callback) and eventdata (a structure of event data).

If you want to invoke the function handle with the 3 additional arguments that should be passed to it (1, 1, and a 1-by-6 array), you need to also pass arguments for the hObject and eventdata inputs. Here's how calling the function would look (using your variable ans):

ans{1}(hButton,[],ans{2:end});

You first get the function handle from the cell array (ans{1}) then call it using parentheses as you would any other function. For hObject you can pass the handle to the uicontrol object (or an empty value if it isn't needed), and for eventdata you can just pass an empty value. The additional values are then taken from the cell array as a comma-separated list (ans{2:end}) and each is passed to the function as a separate additional argument.

走过海棠暮 2024-08-20 03:38:47

您可以使用 () 运算符调用它,也可以将其传递给 feval。您需要首先从元胞数组中提取它。

x; % holds your ans from original question
fcn = x{1}; % Extract from cell array
fcn(); % call with () syntax
feval(fcn); % call with feval() syntax

如果这不起作用,请发布确切的代码和错误消息,以便我们了解出了什么问题。

You can call it with the () operator, or you can pass it to feval. You'll need to extract it from the cell array first.

x; % holds your ans from original question
fcn = x{1}; % Extract from cell array
fcn(); % call with () syntax
feval(fcn); % call with feval() syntax

If that doesn't work, please post exact code and error message so we can see what's going wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文