Brian
Brian

Reputation: 23

MATLAB Array Issue

I am currently writing a MATLAB GUI that will give a multiple-choice test. The way it is designed to work is as follows:

In order to keep track of the questions that are answered wrong or right, I use an array of 1's and 0's (1 is correct, 0 is incorrect). Each index position in the array represents the corresponding question (the array is called rightWrong, rightWrong(1) = score on Q1, etc.). My issue is that, regardless of whether I set the next position in the rightWrong array to 1 or 0, it will set all of the previous values to 0.

The GUI consists of a static text box at the top, a button group with four radio buttons in the center, two pushbuttons on the bottom, and two checkboxes on the left. During the OpeningFcn, I set submitButton (the button that submits the user's answer to the question) and the button group as invisible. After startButton (the button that starts the exam and brings up the first question) is pressed, it is set as invisible along with the check boxes while making submitButton and the button group as visible. This format is used for the rest of the program to ask each question and receive the user's input.

Here is the code for the pertinent section. The line separates the two sub-functions.

% --- 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;

Sorry there is so much, I had to do a lot of visibility changing with the GUI components. If anyone knows of a better way to do this, please let me know.

Thank you!

Upvotes: 2

Views: 602

Answers (1)

bdecaf
bdecaf

Reputation: 4732

Quite complex code ;)

One thing I see is that rightWrong is not declared in the function submitButton_Callback. So it will be recreated everytime the function is called. You should be able to fix it by adding persistent rightWrong at the start of the function. Though it depends how you want to readout the results...

On a personal note - the use of globals is discouraged - and can quite mess app your app if a second instance is started. A good alternative is to use getappdata - setappdata instead.

Upvotes: 1

Related Questions