临时禁用后重启matlab窗口按钮动作

时间:2016-05-24 11:38:04

标签: matlab matlab-figure

我正在设计一个Matlab GUI。在这个GUI中,windowButtonMotion不断地输入。但是,我需要暂停一段时间并向用户询问另一个人物的视觉输入。这个数字在输入后关闭,程序应该以相同的方式工作。

当弹出新图时,我使用此代码禁用WBM:

Sub grades()

Dim wb As Workbook
Dim ws As Worksheet

Dim firstrow, lastrow As Integer

Dim firstAddress As String
Dim tmpFind As Range
Dim grade As String
Dim dateLeft As Double
Dim dateOld As Double

Dim found As Boolean

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Grades")    'change "Grades" to the name of your sheet


    With ws

        firstrow = 2

        lastrow = .Range("A1000000").End(xlUp).Row

        For r = firstrow To lastrow

            found = False

            grade = .Range("A" & r).Value
            dateLeft = .Range("B" & r).Value

            Set tmpFind = .Range("D" & firstrow & ":D" & lastrow).Find(grade, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False)

            If Not tmpFind Is Nothing Then

                firstAddress = tmpFind.Address

                Do

                    dateOld = .Range("E" & tmpFind.Row).Value

                    'check if dateOld is within + - two weeks. if + 2 weeks not required, remove 'And dateOld <= dateLeft + 14' from below
                    If dateOld >= dateLeft - 14 And dateOld <= dateLeft + 14 Then

                        .Range("F" & tmpFind.Row).Value = .Range("C" & r).Value     'writes ID-left to ID-right
                        found = True

                    End If

                    Set tmpFind = .Range("D" & firstrow & ":D" & lastrow).FindNext(tmpFind)

                Loop While Not tmpFind Is Nothing And tmpFind.Address <> firstAddress And found = False

            End If

        Next r

    End With

End Sub

问题是,如何在输入后重新启动WBM。

我没有使用类似函数调用WBM:

set(fighandle, 'WindowButtonMotionFcn', '');

相反,我在GUI中的set(fighandle, 'WindowButtonMotionFcn', @fun); 回调下编写代码。

2 个答案:

答案 0 :(得分:0)

最简单的方法是在更改之前存储回调,然后将其重置为同一个东西。无论你的回调是什么,这都会有效。

% Store the original
originalCallback = get(fighandle, 'WindowButtonMotionFcn');

% Disable the callback
set(fighandle, 'WindowButtonMotionFcn', '')

% Do stuff

% Now reset it
set(fighandle, 'WindowButtonMotionFcn', originalCallback)

如果在同一功能中没有发生这些,您可以将之前的回调存储在图形的应用数据中。

% Get original callback and store within the appdata of the figure
setappdata(fighandle, 'motioncallback', get(fighandle, 'WindowButtonMotionFcn'));

% Disable the callback
set(fighandle, 'WindowButtonMotionFcn', '')

% Then from another function re-enable it
set(fighandle, 'WindowButtonMotionFcn', getappdata(fighandle, 'motioncallback'))

你的另一个选择是实际打开你的第二个数字作为模态数字,然后鼠标与背景图形的交互将被忽略。然后,您将不必篡改WindowButtonMotionFcn

fig2 = figure('WindowStyle', 'modal');

答案 1 :(得分:0)

另一个答案很好,但我找到了另一种效果很好的方法,并且对我的情况更简单:

基本上,我只是将我不想与之交互的图形暂时设置为不可见:

set(fig_handle, 'visible', 'off')

在我的情况下,在设置图形时需要几秒钟来完成所有设置和计算,如果有人碰巧在图形上移动鼠标,因为这一切都在设置,windowbuttonmotionfcn 会出错,所以我只是将数字设置为不可见,直到所有设置完成,然后执行以下操作:

set(fig_handle, 'visible', 'on')

这可能是也可能不是您希望在您的情况下发生的情况。