Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays MATLAB阵列问题_Arrays_User Interface_Matlab_Vector_Octave - Fatal编程技术网

Arrays MATLAB阵列问题

Arrays MATLAB阵列问题,arrays,user-interface,matlab,vector,octave,Arrays,User Interface,Matlab,Vector,Octave,我目前正在编写一个MatlabGUI,它将进行多项选择测试。其设计工作方式如下: 为了跟踪回答错误或正确的问题,我使用了1和0的数组(1是正确的,0是不正确的)。数组中的每个索引位置表示相应的问题(该数组称为RightError,RightError(1)=Q1得分,等等)。我的问题是,无论我将RightError数组中的下一个位置设置为1还是0,它都会将之前的所有值设置为0 GUI由顶部的静态文本框、中间有四个单选按钮的按钮组、底部的两个按钮和左侧的两个复选框组成。在OpeningFcn期间,

我目前正在编写一个MatlabGUI,它将进行多项选择测试。其设计工作方式如下:

为了跟踪回答错误或正确的问题,我使用了1和0的数组(1是正确的,0是不正确的)。数组中的每个索引位置表示相应的问题(该数组称为RightError,RightError(1)=Q1得分,等等)。我的问题是,无论我将RightError数组中的下一个位置设置为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;
%---按submitButton中的按钮时执行。
函数submitButton_回调(hObject、eventdata、handles)
%hObject句柄到submitButton(参见GCBO)
%eventdata保留-将在未来版本的MATLAB中定义
%带有句柄和用户数据的句柄结构(请参见GUI数据)
全局计数器问题答案名称日期qNum
%这是我认为出现问题的部分。右边的数组是错误的
%就是有问题的那个。按钮A到D是单选按钮
%位于相应答案选择的旁边
如果qNum==1
对错=一(1,长度(答案));
结束
if(get(handles.buttonA,'Value')==1)和&(answers(qNum)==a)
对错(1,qNum)=1
elseif(get(handles.buttonB,'Value')==1)和&(answers(qNum)='b'))
对错(1,qNum)=1
elseif(get(handles.buttonC,'Value')==1)和&(answers(qNum)='c')
对错(1,qNum)=1
elseif(get(handles.buttonD,'Value')==1)和&(answers(qNum)==d)
对错(1,qNum)=1
其他的
RightError(1,qNum)=0
结束
计数器=计数器+1;
如果计数器<长度(问题)
集合(handles.textBox、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonA、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonB、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonC、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonD、'String',问题{counter});
qNum=qNum+1
else%此“else”语句可以忽略:问题在解决之前就出现了
%触发。
如果(长度(名称)~=0)和&(长度(日期)~=0)
newGUI(对错、名称、日期)
elseif(长度(名称)~=0)和&(长度(日期)==0)
newGUI(对错,名称)
elseif(长度(名称)==0)和&(长度(日期)~=0)
newGUI(对错,日期)
elseif(长度(名称)==0)和&(长度(日期)==0)
newGUI(对错)
结束
结束
_______________________________________________________________________________________
%---在开始按钮中按按钮时执行。
函数startButton_回调(hObject、eventdata、handles)
%HOBOT手柄至启动按钮(参见GCBO)
%eventdata保留-将在未来版本的MATLAB中定义
%带有句柄和用户数据的句柄结构(请参见GUI数据)
全局问题计数器qNum
计数器=1;
%隐身
设置(handles.startButton、'Visible'、'off');
设置(handles.checkID,'Visible','off');
set(handles.editID,'Visible','off');
设置(handles.checkDate、'Visible'、'off');
设置(handles.editDate、'Visible'、'off');
%看得见
set(handles.choiceGroup,'Visible','on');
set(handles.submitButton,'Visible','on');
set(handles.text1,'Visible','on');
set(handles.text2,'Visible','on');
设置(handles.text3,'Visible','on');
设置(handles.text4,'Visible','on');
%本节列出了第一个问题以及
%所有可能的答案都可以通过各自的单选按钮进行选择。
集合(handles.textBox、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonA、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonB、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonC、'String',问题{counter});
计数器=计数器+1;
集合(handles.buttonD、'String',问题{counter});
qNum=1;
很抱歉,有这么多,我不得不对GUI组件进行很多可见性更改。如果有人知道更好的方法,请告诉我

谢谢大家!

相当复杂的代码;)

我看到的一件事是
rightError
未在函数
submitButton\u Callback
中声明。因此,每次调用函数时都会重新创建它。您应该能够通过在函数的开头添加
persistent rightError
来修复它。虽然这取决于你想如何读出结果


就个人而言,不鼓励使用globals,如果启动第二个实例,它会让应用程序变得一团糟。一个好的替代方法是使用
getappdata
-
setappdata

更正:在第2段中,它应该说“我的问题是,无论我是将RightError数组中的下一个位置设置为1还是0,它都会将之前的所有值设置为0。”我不小心写了“索引值”而不是“值”。数组将保持相同的长度,但所有以前的值都设置为零。请检查
qNum
global的实际值。它是标量还是向量?听起来像是其他代码使用了
qNum=1:index
,而不是
qNum=index
。同意,除非在代码中的其他地方修改了
qNum
RightError
,否则无法看到您发布的内容有任何错误