0

If 4 'Filloval' appear, you must select all 4 ovals. If the same oval is selected 4 times, only 1 oval is selected, so the code should not terminate. However, my current code terminates when selecting the same oval four times.

We created a matrix, and if we click on one oval, we're going to erase the column that corresponds to one oval so that it doesn't end if we press one oval in duplicate.

The problem is that if you click one oval in the (14) matrix to create a (13) matrix, the (13) matrix must be maintained, but it does not.

for mouse = 1 : 4  % mouse 4 click
    
    ShowCursor (0); 
    SetMouse(xCenter, yCenter*1.9, windowPtr);   

    click = 1;
        
            while click == 1;

            [xMouse, yMouse, buttons] = GetMouse(windowPtr);

            inside1 = IsInRect(xMouse, yMouse, Tchoicecenter);
            inside2 = IsInRect(xMouse, yMouse, Tchoicecenter2);
            inside3 = IsInRect(xMouse, yMouse, Tchoicecenter3);
            inside4 = IsInRect(xMouse, yMouse, Tchoicecenter4);
            
            if inside1 == 1 
               Rectlocation(:,mouse) = Tchoicecenter;  
               if any(buttons)
                  break
               end
            elseif inside2 == 1
               Rectlocation(:,mouse) = Tchoicecenter2; 
               if any(buttons)
                  break
               end
            elseif inside3 == 1
               Rectlocation(:,mouse) = Tchoicecenter3; 
               if any(buttons)
                  break
               end
            elseif inside4 == 1
               Rectlocation(:,mouse) = Tchoicecenter4; 
               if any(buttons)
                  break
               end       
            end

            Tovalcenter = CenterRectOnPoint(ovalBaseRect, TXpos, TYpos);
            Tovalcenter2 = CenterRectOnPoint(ovalBaseRect, TXpos2, TYpos2);
            Tovalcenter3 = CenterRectOnPoint(ovalBaseRect, TXpos3, TYpos3);
            Tovalcenter4 = CenterRectOnPoint(ovalBaseRect, TXpos4, TYpos4);

            Screen('FillOval', windowPtr, TColor, Tovalcenter); 
            Screen('FillOval', windowPtr, TColor, Tovalcenter2); 
            Screen('FillOval', windowPtr, TColor, Tovalcenter3); 
            Screen('FillOval', windowPtr, TColor, Tovalcenter4); 
            end

   mouseans = [1:4 ; inside1 inside2 inside3 inside4];
       
   a = find(mouseans(2,:) == 1);     % mouse click (True == 1)
   mouseans(:,a) = []

end

My current result:

mouseans = 1 3 4         % 2 oval click
           0 0 0

mouseans = 1 2 3         % 4 oval click
           0 0 0

mouseans = 1 2 4         % 3 oval click
           0 0 0

mouseans = 2 3 4         % 1 oval click
           0 0 0

The desired result:

mouseans = 1 3 4         % 2 oval click
           0 0 0

mouseans = 1 3           % 4 oval click
           0 0 

mouseans = 1             % 3 oval click
           0 

mouseans = []            % 1 oval click
           

How can I click all four ovals without the program terminating?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 1
    The problem is that you do `mouseans = [1:4 ; inside1 inside2 inside3 inside4];` *for every mouse click*. Thus, you overwrite the previous instance. What you can do is set `mouseans = [1:4];` before the loop, and then `mouseans = [mouseans; inside1 inside2 inside3 inside4];` within the loop. Elimination of columns is difficult when removing elements, e.g. you can't remove the fourth column twice. I'd recommend instead of removing the column to set it to `nan`: `mouseans(:,a) = nan` and be done when the full matrix contains only `nan` values. – Adriaan Sep 22 '20 at 08:09
  • Additionally, this code currently gives you four clicks, thus if you click the same button four times, there should be 3 non-nan columns. However, the code will stop after four clicks, due to that condition being fixed in the `for` declaration. I suggest to ase a `while true` loop, and `if all(isnan(mouseans(:))); break; end` to leave the user to click as often as they want, up to the moment all ovals are clicked, rather than having to stop at four clicks, which might leave ovals unchecked, e.g. by clicking a single oval four times. – Adriaan Sep 22 '20 at 08:17
  • In your (now-deleted) self answer, set just the desired rows to `nan`, i.e. `mouseans([1 i],a) = nan` would almost do what you want. Depending on whether you actually need all next rows of a click to be nan as well, it'll be more difficult. – Adriaan Sep 22 '20 at 08:46
  • Note that the "answer" box should only be used for **answers**. If you want to add more information to the question (please do), [edit] the question instead. Either by clicking the link in this comment, or by clicking the [edit] button beneath the tags on your question. – Adriaan Sep 22 '20 at 08:49
  • I solved this problem. Thank you! – user14319398 Sep 22 '20 at 09:04
  • Feel free to post an answer in that case, which details how you solved the problem. It's perfectly OK to answer your own question, see [Can I answer my own question?](https://stackoverflow.com/help/self-answer). – Adriaan Sep 22 '20 at 09:06

0 Answers0