MATLAB: Save while loop data

loopswhile loop

If I have a while loop that records certain data points, I know how to fprintf in each time the loop goes around, but how can I save these to a matrix without the values getting replaced each time? The loop records the (x,y) coordinates of the cursor every time I click the mouse, and I want to export this into a matrix with all the x values and with all the y values if possible.
Thank you!

Best Answer

You can do it like this:
% first initialize myCoordList to an empty matrix
myCoordList=[];
while (some condition)
% some code that get's your x and y code
myCoordList=[myCoordList; [x, y]];
end
This way as you get more x and y pairs another row is added to myCoordList.
instead of using
myCoordList=[myCoordList; [x, y]];
you can also use:
myCoordList(end+1,:)=[x, y];
(Just use one of them not both)