问题描述
我在Matlab中编程我的第一个GUI。我已经构建了GUI的视觉方面,我现在正在编写回调函数的过程。当我写回调函数,我需要传递两个句柄,但由于某种原因只有一个句柄传递给函数。我相信这是一个基本的问题,但我真的很难过去,我在文档中找不到任何原因,为什么这不工作
I'm writing my first GUI programmatically in Matlab. I have built the visual aspect of the GUI and I am now in the process of writing the Callback functions. When I write the callback function I need to pass two handles to it, but for some reason only one of the handles is passed to the function. I'm sure this is really quite a basic question but I am really struggling to get past this, and I can't find any reason in the documentation why this wouldn't work
回调是针对一个按钮,需要两个用户输入的参数,然后使用这些数字构建一个传递函数。这两个参数被输入到可编辑的文本框中,带有句柄orderEdit和timeConstEdit。我可以提取我需要的字符串,然后将其转换为数字数据格式。代码如下
The callback is for a button and takes two user inputted parameters, and then uses these numbers to build a transfer function. The two parameters are entered into editable text boxes, with handles orderEdit and timeConstEdit. I can extract the string which I need and then convert it to an numeric data format. The code is as follows
首先我将回调标志设置为按钮rtdButton
First I set the callback flag to the button rtdButton
set( rtdButton, 'Callback', @rtdPlot );
回调函数的前两行如下(其余的函数应该工作一次可以得到这些值):
The first two lines of the callback function are as follows (the rest of the function should work once I can get these values):
function rtdPlot(orderEdit,timeConstEdit)
n = str2num(get( orderEdit, 'String' ));
tau = str2num(get( timeConstEdit, 'String' ));
但是我收到错误
??? Error using ==> str2num at 33
Requires string or character array input.
Error in ==> mixingModel>rtdPlot at 148
tau = str2num(get( timeConstEdit, 'String' ));
??? Error while evaluating uicontrol Callback
任何有关如何执行此操作的帮助将非常感谢
Any help on how to do this would be greatly appreciated
编辑:基于Bee的帖子,正确的代码如下:
Based on Bee's post the correct code is as follows:
set(rtdButton,'Callback',{@rtdPlot,
set( rtdButton, 'Callback', {@rtdPlot,orderEdit, timeConstEdit} );
function rtdPlot(src,eventdata,arg1,arg2)
n = str2num(get( orderEdit, 'String' ));
tau = str2num(get( timeConstEdit, 'String' ));
干杯
John
CheersJohn
推荐答案
从Matlab文档:
function myCallback(src,eventdata,arg1,arg2)当使用额外的
参数作为回调函数时,将
属性的值设置为单元格数组(即,用大括号括起函数句柄和
参数):
function myCallback(src,eventdata,arg1,arg2) When using additional arguments for the callback function, you must set the value of the property to a cell array (i.e., enclose the function handle and arguments in curly braces):
WindowButtonDownFcn',{@ myCallback,arg1,arg2})
figure('WindowButtonDownFcn',{@myCallback,arg1,arg2})
这篇关于Matlab回调函数只有一个参数传递给它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!