Hi Daniel,
I'd suggest to break down your problem in tinier pieces, it is hard for us to easily understand your code because there are no comments.
- You can merge task one with two, by tracking the number of clicks, if it's the first click, then it's time to initialise the matrix. Otherwise, if any field in the matrix have different value from the default ones, then initial matrix shouldn't be generated.
- Let's say that you have generated a matrix from the step 1. and by clicking on one button, it will generate an array of elements (mine fields) to be uncovered. Depending on the element values you can generate a list of actions: if the field is mine, display M on button, if it's empty, change color, if it's next to mines, display number, and lastly, if it's mine.
- You can use condition of ButtonDownFcn (right mouse click) to detect if user wants to place a mark, but it is not easy to detect on which field your cursor is on. You can have a toggle button for mine, which might be impractical.