我的matlab while循环无法识别我的功能按钮按下

时间:2016-04-05 08:56:49

标签: matlab while-loop

我有一个脚本在按下按钮时运行,导致启动while循环。按下我的停止按钮时,应该导致循环条件不真实,从而停止循环。在测试期间,我的停止按钮似乎没有停止循环。看起来我在pushDisconnect按钮中分配的值没有更新到while循环,我不知道为什么会发生这种情况。

% --- Executes on button press in pushConnect.
function pushConnect_Callback(hObject, eventdata, handles)
% hObject    handle to pushConnect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.pushDisconnect,'Enable','on')
set(handles.pushConnect,'Enable','off')

handles.myDevice.StartAcquisition;
sampleRate_BP = double(handles.myDevice.BioPotentialSignals.SamplesPerSecond);
axis_handles = zeros(1,handles.numEnabledBPChannels);
BioPotentialSignals = cell(1,handles.numEnabledBPChannels);
handles.CheckFinger = cell(1,5);
handles.stopNow = 0;

for ch = 1:handles.numEnabledBPChannels
    axis_handles(ch) = subplot(length(axis_handles),1,ch,'Parent',handles.uipanelGraph);
    if ch==1
        title(char(handles.deviceName))
    end
    ylabel([char(handles.myDevice.BioPotentialSignals.Item(ch-1).Name) ' (V)']);
    hold on
end

xlabel('Time (s)')
linkaxes(axis_handles,'x')

plotWindow = 5;
plotGain_BP = 1;


while handles.stopNow == 0 
    for ch = 1:handles.numEnabledBPChannels
        BioPotentialSignals{ch} = [BioPotentialSignals{ch};handles.myDevice.BioPotentialSignals.Item(ch-1).GetScaledValueArray.double'];
        if length(BioPotentialSignals{ch}) <= plotWindow*sampleRate_BP
            cla(axis_handles(ch))
            t = (0:(length(BioPotentialSignals{ch})-1))*(1/sampleRate_BP);
            plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch});
            xlim([0 plotWindow])
        else
            if ch==1
                t = ((length(BioPotentialSignals{ch})-(plotWindow*sampleRate_BP-1)):length(BioPotentialSignals{ch}))*(1/sampleRate_BP);
            end
            cla(axis_handles(ch))
            plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch}(end-plotWindow*sampleRate_BP+1:end));
            xlim([t(end)-plotWindow t(end)])
        end
    end
    % Update handles structure
    guidata(hObject, handles);
    pause(0.1)
    disp(handles.stopNow)
end

% Stop signal
handles.myDevice.StopAcquisition;
% Disconnect from all sensors
handles.myDevice.Disconnect;

msgbox('Device requires cycling, in order to restablish connection. Closing program.')

% Close window
close all;

% --- Executes on button press in pushDisconnect.
function pushDisconnect_Callback(hObject, eventdata, handles)
% hObject    handle to pushDisconnect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.pushDisconnect,'Enable','off')

handles.stopNow = 1;

% Update handles structure
guidata(hObject, handles);

最终工作守则,如果有人有兴趣:

% --- Executes on button press in pushConnect.
function pushConnect_Callback(hObject, eventdata, handles)
% hObject    handle to pushConnect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.pushDisconnect,'Enable','on')
set(handles.pushConnect,'Enable','off')

handles.myDevice.StartAcquisition;
sampleRate_BP = double(handles.myDevice.BioPotentialSignals.SamplesPerSecond);
axis_handles = zeros(1,handles.numEnabledBPChannels);
BioPotentialSignals = cell(1,handles.numEnabledBPChannels);
handles.CheckFinger = cell(1,5);
handles.stopNow = 0;


for ch = 1:handles.numEnabledBPChannels
    axis_handles(ch) = subplot(length(axis_handles),1,ch,'Parent',handles.uipanelGraph);
    if ch==1
        title(char(handles.deviceName))
    end
    ylabel([char(handles.myDevice.BioPotentialSignals.Item(ch-1).Name) ' (V)']);
    hold on
end

xlabel('Time (s)')
linkaxes(axis_handles,'x')

plotWindow = 5;
plotGain_BP = 1;

% Update handles structure
guidata(hObject, handles);


while handles.stopNow == 0
    for ch = 1:handles.numEnabledBPChannels
        BioPotentialSignals{ch} = [BioPotentialSignals{ch};handles.myDevice.BioPotentialSignals.Item(ch-1).GetScaledValueArray.double'];
        if length(BioPotentialSignals{ch}) <= plotWindow*sampleRate_BP
            cla(axis_handles(ch))
            t = (0:(length(BioPotentialSignals{ch})-1))*(1/sampleRate_BP);
            plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch});
            xlim([0 plotWindow])
        else
            if ch==1
                t = ((length(BioPotentialSignals{ch})-(plotWindow*sampleRate_BP-1)):length(BioPotentialSignals{ch}))*(1/sampleRate_BP);
            end
            cla(axis_handles(ch))
            plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch}(end-plotWindow*sampleRate_BP+1:end));
            xlim([t(end)-plotWindow t(end)])
        end
    end
    handles = guidata(hObject);
    drawnow 
    pause(0.1)
end

% Stop signal
handles.myDevice.StopAcquisition;
% Disconnect from all sensors
handles.myDevice.Disconnect;

msgbox('Device requires cycling, in order to restablish connection. Closing program.')

% Close window
close all;


% --- Executes on button press in pushDisconnect.
function pushDisconnect_Callback(hObject, eventdata, handles)
% hObject    handle to pushDisconnect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.pushDisconnect,'Enable','off')

handles.stopNow = 1;

% Update handles structure
guidata(hObject, handles);

1 个答案:

答案 0 :(得分:1)

杀死guidata(hObject, handles)行,并将其替换为handles=guidata(hObject),因为您已使用循环开始时的当前状态覆盖GUI状态,并且您想要轮询GUI相反。在循环之前放置guidata(hObject, handles) - 行,以确保handles.stopNow确实重置为0。

如果这没有帮助,另一个问题可能是Matlab等待处理来自一个按钮回调的命令,直到另一个回调完成运行。

您可以尝试两件事:在暂停前添加drawnow命令。这将强制GUI更新,并处理所有交互。另外,请确保pushConnect的按钮回调是可中断的。