Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab GUI windowButtonMotionFcn在未采取任何操作时变慢_Matlab_User Interface_Matlab Figure_Matlab Guide - Fatal编程技术网

Matlab GUI windowButtonMotionFcn在未采取任何操作时变慢

Matlab GUI windowButtonMotionFcn在未采取任何操作时变慢,matlab,user-interface,matlab-figure,matlab-guide,Matlab,User Interface,Matlab Figure,Matlab Guide,我有一个带有windowButtonMotionFcn回调的MATLAB(r2016a)GUI。回调内部有一个if-elseif-else块,它根据鼠标在GUI中的哪个轴上移动来更改光标。当遇到需要更改某些内容的情况时,它运行得非常快,但根据profiler的说法,“return”命令需要花费大量时间 有人能提供一种方法来纠正这个问题吗?在我看来,如果它没有执行更多的代码,那么当它退出函数时(即,没有挂断“返回”)应该比当它有更多的代码要执行时运行得更快 守则的基本内容如下: function

我有一个带有windowButtonMotionFcn回调的MATLAB(r2016a)GUI。回调内部有一个if-elseif-else块,它根据鼠标在GUI中的哪个轴上移动来更改光标。当遇到需要更改某些内容的情况时,它运行得非常快,但根据profiler的说法,“return”命令需要花费大量时间

有人能提供一种方法来纠正这个问题吗?在我看来,如果它没有执行更多的代码,那么当它退出函数时(即,没有挂断“返回”)应该比当它有更多的代码要执行时运行得更快

守则的基本内容如下:

function mouseMove(handles)

xy = %Get cursor position

if %xy over axes1
    set(gcf,'Pointer','crosshair')
elseif %xy over axes2
    set(gcf,'Pointer','arrow')
else
    return %Here is where MATLAB is spending a lot of time
end

%A lot of additional code for when the cursor is over axes1 or axes2.

end

这可能与以下事实有关:。一般来说,探查器可以很好地识别代码中的低效性(调用次数太多的函数等),但实际上并不适合对代码进行基准测试,因为没有启用JIT加速。对于基准测试,最好使用
timeit
或其他一些功能

也就是说,当我实现相同的回调时,我没有看到
return
语句的性能受到任何影响


正如@Ander指出的,您应该注意Calls列,因为调用
return
的次数可能比回调的其余部分要多。给定行的默认颜色基于总时间,而不是每次调用的时间。

我不知道探查器如何为GUI工作,但您确定该行不会花费更多时间,因为它运行的频率更高,即通常xy不在轴上吗?你是说每次通话时间更长还是总共更长?你确定要在那里回程吗?这不会导致代码在到达“当光标位于axes1/axes2上时的附加代码”之前退出mouseMove函数吗?@AnderBiguri这是可能的。我将对此进行调查。结果表明,每个案例的每次通话时间大致相同。为了解决性能问题,我添加了一个检查,查看上次调用函数的时间,如果小于10毫秒,则返回。这使得该功能运行平稳。