我正在編寫一個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或0,它都會將所有先前的值設置爲0」。我不小心寫了「索引值」而不是「值」。該數組將保持相同的長度,但所有以前的值都設置爲零。 – Brian
檢查'qNum'全局的實際值。它是標量還是矢量?這聽起來像是其他一段代碼使用'qNum = 1:index',而不是'qNum = index'。 – Pursuit
同意,不能看到你發佈的內容有什麼問題,除非'qNum'或'rightWrong'在代碼的其他地方被修改 – tdc