【发布时间】:2012-01-11 04:53:24
【问题描述】:
我目前正在编写一个可以进行多项选择测试的 MATLAB GUI。它的设计工作方式如下:
为了跟踪回答错误或正确的问题,我使用了一个由 1 和 0 组成的数组(1 是正确的,0 是不正确的)。数组中的每个索引位置代表相应的问题(数组称为 rightWrong、rightWrong(1) = 在 Q1 上的得分等)。我的问题是,无论我将 rightWrong 数组中的下一个位置设置为 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 组件更改很多可见性。如果有人知道更好的方法,请告诉我。
谢谢!
【问题讨论】:
-
更正:在第二段中,它应该说“我的问题是,无论我将 rightWrong 数组中的下一个位置设置为 1 还是 0,它都会将所有先前的值设置为0。”我不小心写了“索引值”而不仅仅是“值”。数组将保持相同的长度,但所有先前的值都设置为零。
-
检查
qNum全局的实际值。它是标量还是向量?听起来其他一些代码正在使用qNum = 1:index,而不是qNum = index。 -
同意,看不到您发布的内容有任何问题,除非在代码的其他地方修改了
qNum或rightWrong
标签: arrays user-interface matlab vector octave