The error is likely because the code is trying to execute after you closed the figure window. Closing the figure destroys the axes, making the call to "axes" throw that error.
There are two simple fixes, and you can technically do both. The first is to add a "CloseRequestFcn" to the figure that changes a property that is checked by the while loop. An easier way is probably just to check that the axes are still valid before trying to use them, and dropping out of the loop if they're not valid:
while keepGoing && isvalid(handles.axes1)
Also, you can specify the axes to plot to directly in "imshow", using the 'Parent' name-value pair. So no need for the call to "axes". You may also consider updating "keepGoing" within the loop, as it currently doesn't seem like it will (unless it's a function call that checks some property or something). Finally, you can call "imshow" directly with the filename, so no need for a separate call to "imread", unless you use the results later.