Matlab GUI / GUIDE将项添加到存储在句柄中的向量

时间:2014-05-27 17:42:48

标签: matlab matlab-guide

我正在制作一个程序,记录用户按下按钮以响应一系列音调。我将按钮按下的时间存储在矢量(pushTimes)中,我将其存储在GUIDE的“句柄”结构中,我希望按下每个按钮的新时间附加到pushTimes矢量。但是,句柄似乎没有存储带有附加值的新向量,在按下按钮后我留下了一个空向量。

编辑:因为人们很难再现错误,我发布整个文件相关代码在callBack pushButton1中。 pushButton2和pushButton3分别是“暂停”和“开始”按钮。即使我没有暂停程序,也会发生错误。

function varargout = simpleGui(varargin)
% SIMPLEGUI M-file for simpleGui.fig
%      SIMPLEGUI, by itself, creates a new SIMPLEGUI or raises the existing
%      singleton*.
%
%      H = SIMPLEGUI returns the handle to a new SIMPLEGUI or the handle to
%      the existing singleton*.
%
%      SIMPLEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SIMPLEGUI.M with the given input arguments.
%
%      SIMPLEGUI('Property','Value',...) creates a new SIMPLEGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before simpleGui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to simpleGui_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 simpleGui

% Last Modified by GUIDE v2.5 27-May-2014 11:00:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @simpleGui_OpeningFcn, ...
                   'gui_OutputFcn',  @simpleGui_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 simpleGui is made visible.
function simpleGui_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 simpleGui (see VARARGIN)
[soundVec, codeVec] = FourToneDifDirection();
handles.soundVec = soundVec;
handles.codeVec = codeVec;
handles.toneTimes = zeros(1,length(soundVec));
handles.pushTimes = [];
handles.toneNum = 1;
% Choose default command line output for simpleGui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);


% UIWAIT makes simpleGui wait for user response (see UIRESUME)




% --- Outputs from this function are returned to the command line.
function varargout = simpleGui_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 pushbutton1.
function handles = pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
dv = datevec(now);
%get the seconds from datevec
sec = dv(6);
%append to the pushTimes vector
handles.pushTimes = [handles.pushTimes sec]
%debugging line, should return a large vector after button is pressed multiple times     but only returns a single value.
handles.pushTimes
guidata(hObject, handles)

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
UIresume
guidata(hObject, handles);
soundVec = handles.soundVec
n = 1;
while(n<length(soundVec))
    guidata(hObject, handles);
    n = handles.toneNum
    midigen(handles.soundVec(n), 0.25);
    dv = datevec(now);
    sec = dv(6);
    handles.toneTimes(n) = sec;
    n = n+1;
    handles.toneNum = n;
end
guidata(hObject, handles)



% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
guidata(hObject, handles);
codeVec = handles.codeVec;
toneTimes = handles.toneTimes;
pushTimes = handles.pushTimes
calculateHitRate(codeVec, toneTimes, pushTimes,1,handles.toneNum);
guidata(hObject, handles)
uiwait(handles.figure1);

function out = isWithin(x, low, high)
   out = x > low & x < high;


function [d1hr, d2hr, d3hr, d4hr] = calculateHitRate(codeVec, toneTimes, pushTimes, start, finish)
    d1Ct = sum(codeVec(start:finish) == 9);
    d2Ct = sum(codeVec(start:finish) == 10);
    d3Ct = sum(codeVec(start:finish) == 11);
    d4Ct = sum(codeVec(start:finish) == 12);
    d1Push = 0;
    d2Push = 0;
    d3Push = 0;
    d4Push = 0;
    for i = 1:length(pushTimes)
        pushTime = pushTimes(i)
        stimTime = pushTime - 2;
        for j = start:finish
            %if the stimulus was within 1.5 seconds before the push
            if codeVec(j) == 9 && isWithin(toneTimes(j), stimTime, pushTime)
                d1Push = d1Push+1;
            elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
                d2Push = d2Push+1;
            elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
                d3Push = d3Push+1;
            elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
                d4Push = d4Push+1;
            end
        end
    end
    d1hr = d1Push/d1Ct
    d2hr = d2Push/d2Ct
    d3hr = d3Push/d3Ct
    d4hr = d4Push/d4Ct

0 个答案:

没有答案