只有我的最后一个子图显示通过句柄访问GUI时

时间:2016-04-01 04:41:06

标签: matlab matlab-figure matlab-guide

我有一个功能,它将设备的读数显示在我的GUI上。目前存在一个问题,即只有最后一个子图显示在GUI图中,在GUI中它自己显示空白区域(没有轴和图形数据),其他三个图应该是。第四个子图显示了所有数据。我已经测试了这个功能,没有使用设备中的数据,而是在自己显示所有子图的功能中启动图形(不在GUI中)。

axes1是GUI中的我的轴标记。

function  BioRadioData = BioRadio_Stream2( myDevice , duration , BioRadio_Name , axes1 )


numEnabledBPChannels = double(myDevice.BioPotentialSignals.Count);

if numEnabledBPChannels == 0
    myDevice.Disconnect;
    BioRadioData = [];
    errordlg('No BioPotential Channels Programmed. Return to BioCapture to Configure.')
    return
end

sampleRate_BP = double(myDevice.BioPotentialSignals.SamplesPerSecond);
sampleRate_Pod = 250;

axis_handles = zeros(1,numEnabledBPChannels);
disp(axis_handles)
disp(length(axis_handles))
for ch = 1:numEnabledBPChannels
    axis_handles(ch) = subplot(length(axis_handles),1,ch,axes1);
    disp(ch)
    if ch==1
        title(char(BioRadio_Name))
    end
    ylabel([char(myDevice.BioPotentialSignals.Item(ch-1).Name) ' (V)']);
    hold(axes1,'on')
end

xlabel('Time (s)')

linkaxes(axis_handles,'x')


BioPotentialSignals = cell(1,numEnabledBPChannels);

myDevice.StartAcquisition;

plotWindow = 5;

plotGain_BP = 1;

elapsedTime = 0;
tic;

while elapsedTime < duration
    pause(0.08)
    for ch = 1:numEnabledBPChannels
        BioPotentialSignals{ch} = [BioPotentialSignals{ch};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

    elapsedTime = elapsedTime + toc;
    tic;
end

myDevice.StopAcquisition;

BioRadioData = cell(1,1);
BioRadioData{1} = BioPotentialSignals;

end

下面的代码段可以工作,但会为创建的GUI生成一个单独的图形。虽然上面的代码只生成GUI中的第4个图。

function  BioRadioData = BioRadio_Stream2( myDevice , duration , BioRadio_Name )



    numEnabledBPChannels = double(myDevice.BioPotentialSignals.Count);

    if numEnabledBPChannels == 0
        myDevice.Disconnect;
        BioRadioData = [];
        errordlg('No BioPotential Channels Programmed. Return to BioCapture to Configure.')
        return
    end

    sampleRate_BP = double(myDevice.BioPotentialSignals.SamplesPerSecond);
    sampleRate_Pod = 250;

    axis_handles = zeros(1,numEnabledBPChannels);
    disp(axis_handles)
    disp(length(axis_handles))
    figure
    for ch = 1:numEnabledBPChannels
        %axis_handles(ch) = subplot(length(axis_handles),1,ch,axes1);
        axis_handles(ch) = subplot(length(axis_handles),1,ch);
        disp(ch)
        if ch==1
            title(char(BioRadio_Name))
        end
        ylabel([char(myDevice.BioPotentialSignals.Item(ch-1).Name) ' (V)']);
        %hold(axes1,'on')
        hold on
    end

    disp(axis_handles)
    disp(length(axis_handles))
    disp(ch)

    xlabel('Time (s)')

    linkaxes(axis_handles,'x')


    BioPotentialSignals = cell(1,numEnabledBPChannels);

    myDevice.StartAcquisition;

    plotWindow = 5;

    plotGain_BP = 1;

    elapsedTime = 0;
    tic;

    while elapsedTime < duration
        pause(0.08)
        for ch = 1:numEnabledBPChannels
            BioPotentialSignals{ch} = [BioPotentialSignals{ch};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});
                hold(axis_handles(ch),'on')
                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));
                hold(axis_handles(ch),'on')
                xlim([t(end)-plotWindow t(end)])
            end
        end

        elapsedTime = elapsedTime + toc;
        tic;
    end

    myDevice.StopAcquisition;

    BioRadioData = cell(1,1);
    BioRadioData{1} = BioPotentialSignals;

    end

测试数据

function varargout = ProjectGUI5test(varargin)
% PROJECTGUI5TEST MATLAB code for ProjectGUI5test.fig
%      PROJECTGUI5TEST, by itself, creates a new PROJECTGUI5TEST or raises the existing
%      singleton*.
%
%      H = PROJECTGUI5TEST returns the handle to a new PROJECTGUI5TEST or the handle to
%      the existing singleton*.
%
%      PROJECTGUI5TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PROJECTGUI5TEST.M with the given input arguments.
%
%      PROJECTGUI5TEST('Property','Value',...) creates a new PROJECTGUI5TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ProjectGUI5test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ProjectGUI5test_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ProjectGUI5test

% Last Modified by GUIDE v2.5 01-Apr-2016 16:25:01

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ProjectGUI5test_OpeningFcn, ...
                   'gui_OutputFcn',  @ProjectGUI5test_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ProjectGUI5test is made visible.
function ProjectGUI5test_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ProjectGUI5test (see VARARGIN)

% Choose default command line output for ProjectGUI5test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ProjectGUI5test wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ProjectGUI5test_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in connectbutton.
function connectbutton_Callback(hObject, eventdata, handles)
% hObject    handle to connectbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
%loads handles into the function
BioRadioData = BioRadio_Stream3( 30 , handles.axes1 );


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


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1

function  BioRadioData = BioRadio_Stream3( duration , axes1 )
% function  BioRadioData = BioRadio_Stream( myDevice , duration , BioRadio_Name )
% BioRadio_Stream streams data from the BioRadio and imports it into MATLAB.
%
% INPUTS:
% - myDevice is a handle to a BioRadio device object
% - duration is the data collection interval in seconds
% - BioRadio_name is string containing the BioRadio name


numEnabledBPChannels = 4;


sampleRate_BP = 1000;

axis_handles = zeros(1,numEnabledBPChannels);


for ch = 1:numEnabledBPChannels
    axis_handles(ch) = subplot(length(axis_handles),1,ch,axes1);
    disp(ch)
    if ch==1
        title('Title')
    end
    ylabel(' (V)');
    hold(axes1,'on')
end



xlabel('Time (s)')

linkaxes(axis_handles,'x')

BioPotentialSignals = cell(1,numEnabledBPChannels);

plotWindow = 5;

plotGain_BP = 1;

elapsedTime = 0;
tic;

while elapsedTime < duration
    pause(0.08)
    for ch = 1:numEnabledBPChannels
        BioPotentialSignals{ch} = [BioPotentialSignals{ch};sin(t)];
        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});
            hold(axis_handles(ch),'on')
            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));
            hold(axis_handles(ch),'on')
            xlim([t(end)-plotWindow t(end)])
        end
    end

    elapsedTime = elapsedTime + toc;
    tic;
end


BioRadioData = cell(1,1);
BioRadioData{1} = BioPotentialSignals;

2 个答案:

答案 0 :(得分:1)

删除.fig中的轴,然后添加uipanel。

connectbutton_Callback函数中,您调用BioRadio_Stream3。发送您的uipanel作为输入而不是您以前的轴,如下所示:

BioRadio_Stream3( 30 , handles.uipanel1 );
function BioRadio_Stream3

,将subplot(4,1,1,axes1);更改为subplot(4,1,1,'Parent',axes1);

其他子图也一样。

答案 1 :(得分:0)

我设法通过更改代码来解决问题:

for ch = 1:numEnabledBPChannels
    axis_handles(ch) = subplot(length(axis_handles),1,ch);
    disp(ch)

for ch = 1:numEnabledBPChannels
    axis_handles(ch) = subplot(length(axis_handles),1,ch,axes1);
    disp(ch)

在subplot命令中设置轴句柄时,显然matlab不喜欢它。不知道为什么它不喜欢它,matlab文档显示了这种可能的例子。