Note that there are some explanatory texts on larger screens.

plurals
  1. POMATLAB Array Issue
    text
    copied!<p>I am currently writing a MATLAB GUI that will give a multiple-choice test. The way it is designed to work is as follows:</p> <p>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.</p> <p>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.</p> <p>Here is the code for the pertinent section. The line separates the two sub-functions.</p> <pre><code>% --- 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) &amp;&amp; (answers(qNum) == 'a') rightWrong(1,qNum) = 1 elseif (get(handles.buttonB,'Value') == 1) &amp;&amp; (answers(qNum) == 'b') rightWrong(1,qNum) = 1 elseif (get(handles.buttonC,'Value') == 1) &amp;&amp; (answers(qNum) == 'c') rightWrong(1,qNum) = 1 elseif (get(handles.buttonD,'Value') == 1) &amp;&amp; (answers(qNum) == 'd') rightWrong(1,qNum) = 1 else rightWrong(1,qNum) = 0 end counter = counter + 1; if counter &lt; 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) &amp;&amp; (length(date)~=0) newGUI(rightWrong, name, date) elseif (length(name)~=0) &amp;&amp; (length(date)==0) newGUI(rightWrong, name) elseif (length(name)==0) &amp;&amp; (length(date)~=0) newGUI(rightWrong, date) elseif (length(name)==0) &amp;&amp; (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; </code></pre> <p>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.</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload