2011-12-02 77 views
2

我正在编写一个MATLAB GUI,它将提供多选题测试。它的设计工作方式如下:MATLAB Array Issue

为了跟踪回答错误或正确的问题,我使用1和0的数组(1是正确的,0是不正确的)。数组中的每个索引位置表示相应的问题(该数组称为rightWrong,rightWrong(1)= Q1上的分数等)。我的问题是,不管我是否将右边的错误数组中的下一个位置设置为1或0,它将将所有先前的值设置为0.

GUI由顶部的静态文本框,按钮组,中央有四个单选按钮,底部有两个按钮,左侧有两个复选框。在OpeningFcn期间,我将submitButton(提交用户对问题的答案的按钮)和按钮组设置为不可见。按下startButton(启动考试并显示第一个问题的按钮)后,将submitButton和按钮组显示为可见时,将其设置为不可见以及复选框。该格式用于该程序的其余部分来询问每个问题并接收用户的输入。

以下是相关部分的代码。该行分隔了两个子功能。

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

global counter questions answers name date qNum 

% This is the section that I believe the problem occurs in. The rightWrong array 
% is the one with the problem. Buttons A through D are the radio buttons 
% situated next to their corresponding answer choices 

if qNum == 1 
    rightWrong = ones(1, length(answers)); 
end 
if (get(handles.buttonA,'Value') == 1) && (answers(qNum) == 'a') 
    rightWrong(1,qNum) = 1 
elseif (get(handles.buttonB,'Value') == 1) && (answers(qNum) == 'b') 
    rightWrong(1,qNum) = 1 
elseif (get(handles.buttonC,'Value') == 1) && (answers(qNum) == 'c') 
    rightWrong(1,qNum) = 1 
elseif (get(handles.buttonD,'Value') == 1) && (answers(qNum) == 'd') 
    rightWrong(1,qNum) = 1 
else 
    rightWrong(1,qNum) = 0 
end 
counter = counter + 1; 
if counter < length(questions) 
    set(handles.textBox,'String',questions{counter}); 
    counter = counter + 1; 
    set(handles.buttonA,'String',questions{counter}); 
    counter = counter + 1; 
    set(handles.buttonB,'String',questions{counter}); 
    counter = counter + 1; 
    set(handles.buttonC,'String',questions{counter}); 
    counter = counter + 1; 
    set(handles.buttonD,'String',questions{counter}); 
    qNum = qNum + 1 
else % This "else" statement can be ignored: the problem occurs before it is 
    % triggered. 
    if (length(name)~=0) && (length(date)~=0) 
     newGUI(rightWrong, name, date) 
    elseif (length(name)~=0) && (length(date)==0) 
     newGUI(rightWrong, name) 
    elseif (length(name)==0) && (length(date)~=0) 
     newGUI(rightWrong, date) 
    elseif (length(name)==0) && (length(date)==0) 
     newGUI(rightWrong) 
    end 
end 

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

global questions counter qNum 
counter = 1; 

%Make Invisible 
set(handles.startButton,'Visible','off'); 
set(handles.checkID,'Visible','off'); 
set(handles.editID,'Visible','off'); 
set(handles.checkDate,'Visible','off'); 
set(handles.editDate,'Visible','off'); 

%Make Visible 
set(handles.choiceGroup,'Visible','on'); 
set(handles.submitButton,'Visible','on'); 
set(handles.text1,'Visible','on'); 
set(handles.text2,'Visible','on'); 
set(handles.text3,'Visible','on'); 
set(handles.text4,'Visible','on'); 

% This sections lists the First Question as well as 
% all of the possible answers by their respective radio buttons. 
set(handles.textBox,'String',questions{counter}); 
counter = counter + 1; 
set(handles.buttonA,'String',questions{counter}); 
counter = counter + 1; 
set(handles.buttonB,'String',questions{counter}); 
counter = counter + 1; 
set(handles.buttonC,'String',questions{counter}); 
counter = counter + 1; 
set(handles.buttonD,'String',questions{counter}); 
qNum = 1; 

对不起,有这么多,我不得不做很多与GUI组件的可见性变化。如果有人知道更好的方法来做到这一点,请让我知道。

谢谢!

+1

更正:在第二段中,应该说“我的问题是,不管我是否将右侧错误数组中的下一个位置设置为1或0,它都会将所有先前的值设置为0”。我不小心写了“索引值”而不是“值”。该数组将保持相同的长度,但所有以前的值都设置为零。 – Brian

+0

检查'qNum'全局的实际值。它是标量还是矢量?这听起来像是其他一段代码使用'qNum = 1:index',而不是'qNum = index'。 – Pursuit

+0

同意,不能看到你发布的内容有什么问题,除非'qNum'或'rightWrong'在代码的其他地方被修改 – tdc

回答

1

相当复杂的代码;)

一件事,我看到的是, rightWrong没有在功能submitButton_Callback声明。所以每次调用函数时都会重新创建它。您应该可以通过在函数的开始处添加persistent rightWrong来修复它。虽然这取决于你想要如何读出结果...

个人消息 - 使用全局变量不鼓励 - 如果第二个实例启动,可能会让应用程序变得非常混乱。一个好的选择是使用getappdata - setappdata来代替。

+0

谢谢,bdecaf!那正是我的问题。 – Brian