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

matlab matlab-figure matlab-guide

97 观看

2回复

103 作者的声誉

我有一个功能,可以将设备上的读数显示到我的GUI上。目前存在一个问题,即在GUI图形中仅显示了最后一个子图,在GUI中它本身显示了其他三个图应位于的空白(没有轴和图形数据)。第四个子图完美显示了所有数据。我已经在不使用设备数据的情况下测试了此功能,并从该功能本身显示所有子图的功能中启动了图形(不在GUI中)。

axes1是我在GUI中的axes标记。

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;
作者: IamTrent 的来源 发布者: 2016 年 4 月 1 日

回应 (2)


1

833 作者的声誉

决定

移除.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);

其他子图也一样。

作者: jkazan 发布者: 01.04.2016 05:43

0

103 作者的声誉

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

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文档显示了可能的例子。

作者: IamTrent 发布者: 01.04.2016 10:10
32x32