简体   繁体   English

回调 function 抛出意外的“不一致的参数”错误

[英]callback function throws unexpected “nonconformant arguments” error

I'm using GNU Octave, version 4.4.1.我正在使用 GNU Octave,版本 4.4.1。 I'm trying to make an interactive plot of a simple oscillator by including two sliders which would allow the initial velocity and oscillator mass to be changed.我试图通过包括两个滑块来制作一个简单的振荡器的交互式 plot,这将允许改变初始速度和振荡器质量。 The plot itself shows fine, as well as the script with one slider (for velocity). plot 本身显示良好,以及带有一个 slider(用于速度)的脚本。 Here's a part of that script with the callback function:这是带有回调 function 的脚本的一部分:

function titranje_ia1 (hslider, event)

v0 = get ( hslider, "value" );
m = 1;
k = 1;
t = 0:0.1:30;
x = v0*sin(sqrt(k/m)*t);
axes ('position', [0.1, 0.2, 0.8, 0.75]);
h = plot ( t, x );
axis ([0 30 -11 11]);
set (h, "linewidth", 2);
set (gca, "xlabel", "t (s)", "ylabel", "x (m)", "fontsize", 12);
set (gca, 'XTick', 0:pi:10*pi)
set (gca, 'XTickLabel', {'0','\pi','2\pi','3\pi','4\pi','5\pi','6\pi','7\pi','8\pi','9\pi','10\pi'})
grid on;
l = legend (sprintf('v0 = %f', v0));
set (l, "fontsize", 12)

endfunction

However, when I include a second slider但是,当我包含第二个 slider

function titranje_ia2 (hslider1, hslider2, event)

v0 = get ( hslider1, "value" );
m = get ( hslider2, "value" );
k = 1;
t = 0:0.1:30;
x = v0.*sin(sqrt(k./m).*t);
axes ('position', [0.1, 0.2, 0.8, 0.75]);
h = plot ( t, x );
axis ([0 30 -11 11]);
set (h, "linewidth", 2);
set (gca, "xlabel", "t (s)", "ylabel", "x (m)", "fontsize", 12);
set (gca, 'XTick', 0:pi:10*pi)
set (gca, 'XTickLabel', {'0','\pi','2\pi','3\pi','4\pi','5\pi','6\pi','7\pi','8\pi','9\pi','10\pi'})
grid on;
l = legend (sprintf('v0 = %f', v0));
set (l, "fontsize", 12)

endfunction

I receive an error:我收到一个错误:

error: titranje_ia2: product: nonconformant arguments (op1 is 0x0, op2 is 1x301) execution error in graphics callback function错误:titranje_ia2:产品:不合格 arguments(op1 为 0x0,op2 为 1x301)图形回调 function 执行错误

Since I know that 'k' is a scalar and 't' a vector (but I'm not sure what v0 and m would be; I suppose scalars), I included an elementwise operations in function 'x' definition.因为我知道'k'是一个标量而't'是一个向量(但我不确定 v0 和 m 是什么;我想是标量),我在 function 'x' 定义中包含了一个元素操作。 't' size is 1x301, so I assume that 'sqrt(k./m)' is 0x0 (as seen by Octave). 't' 大小是 1x301,所以我假设 'sqrt(k./m)' 是 0x0(如 Octave 所示)。 Shouldn't it be 1x1?不应该是1x1吗? Indeed, when I try确实,当我尝试

size(m)

I receive ans = 0 0 (for size(v0) I get ans = 1 1).我收到 ans = 0 0(对于 size(v0) 我得到 ans = 1 1)。 Could it be that there is a problem with slider definition?莫非是slider定义有问题? I include at the end both slider definitions:我最后包括两个 slider 定义:

%Definiramo ui element: 'klizac' za v0
hslider1 = uicontrol (
"style", "slider",
"units", "normalized",
"position", [0.1, 0.0, 0.8, 0.1],
"min", 1,
"max", 10,
"value", 4,
"callback", @titranje_ia2
);

%Definiramo ui element: 'klizac' za m
hslider2 = uicontrol (
"style", "slider",
"units", "normalized",
"position", [0.1, 0.05, 0.8, 0.1],
"min", 1,
"max", 10,
"value", 1,
"callback", @titranje_ia2
);

Thank You in advance, Best regards,提前谢谢你,最好的问候,

Igor伊戈尔

You seem to be misunderstanding a bit how callbacks work.您似乎对回调的工作方式有点误解。 Whenever you interact with a uicontrol object, the associated callback is always called automatically with [at least] two arguments: the first is always the 'handle' of the corresponding uicontrol object that triggerred he callback, and the second is the type of 'event' that was triggered.每当您与 uicontrol object 交互时,始终使用 [至少]两个arguments 自动调用关联的回调:第一个始终是相应 uicontrol object 的“句柄”,第二个是触发事件回调的类型'这被触发了。 Therefore your callback function needs to always have a signature of callbackname( hndl, evt ) at the very least, so that it can handle those two arguments passed to it by default.因此,您的回调 function 至少需要始终具有callbackname( hndl, evt )的签名,以便它可以处理默认传递给它的这两个 arguments 。

You can specify extra arguments to passed to the callback if you want, but these will necessarily be interpreted as 3rd, 4th, and so on, inside the callback function (see example below).如果需要,您可以指定额外的arguments 传递给回调,但这些必须在回调 function 内被解释为第 3、第 4 等等(参见下面的示例)。

If you have a case like yours where you don't really care where the event originated from, and you want to affect (or in this case 'read from') two existing uicontrols in a single callback, regardless which one triggerred the event, the simplest thing to do is to make both slider handles to be extra arguments to the callback, and simply ignore the first argument (ie the 'active handle') inside your callback.如果您遇到像您这样的情况,您并不真正关心事件的来源,并且您想在单个回调中影响(或在本例中“读取”)两个现有的 uicontrol,无论哪个触发了事件,最简单的做法是使两个 slider 句柄成为回调的额外 arguments ,并简单地忽略回调中的第一个参数(即“活动句柄”)。 (in fact, you're already ignoring the 'event' argument too.). (实际上,您也已经忽略了“事件”参数。)。

Here's an example:这是一个例子:

%% In file makesliders.m
function makesliders()
  % Label and slider for initial velocity
    IV_label  = uicontrol( 'style', 'text'  , 'units', 'normalized', 'position', [0.10, 0.025, 0.30, 0.075], 'string', 'Initial Velocity' );
    IV_slider = uicontrol( 'style', 'slider', 'units', 'normalized', 'position', [0.45, 0.025, 0.50, 0.075], 'min', 1, 'max', 10 );

  % Label and slider for oscillator mass
    OM_label  = uicontrol( 'style', 'text'  , 'units', 'normalized', 'position', [0.10, 0.125, 0.30, 0.075], 'string', 'Oscillator Mass ' );
    OM_slider = uicontrol( 'style', 'slider', 'units', 'normalized', 'position', [0.45, 0.125, 0.50, 0.075], 'min', 1, 'max', 10 );

  % Set callbacks and initial values to IV and OM sliders, and plot initial graph
    v0_init = 4;   m_init = 1;
    set( IV_slider, 'value', v0_init, 'callback', { @slider_callback, IV_slider, OM_slider } );
    set( OM_slider, 'value', m_init , 'callback', { @slider_callback, IV_slider, OM_slider } );
    plot_oscillation( v0_init, m_init );
endfunction

function slider_callback (active_handle, event, IV_slider, OM_slider )
    v0 = get( IV_slider, 'value' );
    m  = get( OM_slider, 'value' );
    plot_oscillation( v0, m );
endfunction

function plot_oscillation( v0, m )
    k  = 1;
    t  = 0 : 0.1 : 30;
    x = v0 * sin( sqrt( k / m ) * t );

    h = plot( t, x );   set( h  , 'linewidth', 2);
    set( gca, 'position', [0.1, 0.325, 0.85, 0.650], 'xlim', [0, 30], 'ylim', [-11, 11], 'xlabel', 't (s)', 'ylabel', 'x (m)', 'fontsize', 12, 'xtick', [0 : pi : 10 * pi], 'xticklabel', strcat( arrayfun( @num2str, 0:10, 'uniformoutput', false ), '\pi' ), 'xgrid', 'on', 'ygrid', 'on' );
    l = legend( sprintf( 'v0 = %.2f, m = %.2f', v0, m ) );   set( l, 'fontsize', 12 );
endfunction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此高斯函数会给出不一致的参数错误? - Why does this Gaussian function give nonconformant arguments error? 当我不使用运算符时,如何在Octave中修复不一致的参数错误? - How can I fix a nonconformant arguments error in Octave when I am not using an operator? 请说明Octave-Error:运算符/:不符合参数(op1为1x1,op2为1x10) - Please Explain Octave-Error : operator /: nonconformant arguments (op1 is 1x1, op2 is 1x10) Octave:如何为现有单元格数组分配正确的维度以避免“错误:=:不合格 arguments(op1 为 2x2x2,op2 为 2x2x2)”? - Octave: How to assign the right dimensionality to an existing cell array to avoid “error: =: nonconformant arguments (op1 is 2x2x2, op2 is 2x2x2)”? Octave 和 imnoise 的问题(不一致 arguments(op1 是 11x1,op2 是 9x1)) - Problem with Octave and imnoise (nonconformant arguments (op1 is 11x1, op2 is 9x1)) 带sum()函数的异常行为 - Unexpected behavior w/ sum() function 倍频gnuplot在plot命令上抛出无数据错误 - octave gnuplot throws a no data error on plot command sparse.spdiags()发生意外错误 - Unexpected error from sparse.spdiags() 八度绘图中 datetick function 的意外行为 - Unexpected behavior of datetick function in Octave plotting 如何使Octave将函数参数解释为数字? - How to make Octave interpret function arguments as numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM