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!

 채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 10월 23일
편집: Mohammad Abouali 2015년 10월 23일

8 개 추천

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)

댓글 수: 8

Katerina Tetzloff
Katerina Tetzloff 2015년 10월 23일
Thank you so much!!
Mohammad Abouali
Mohammad Abouali 2015년 10월 23일
You are welcome.
bipul
bipul 2019년 1월 29일
It works myCoordList(end+1,:)=[x, y];
I found this really helpful
myCoordList(end+1,:)=[x, y];
Just what i was having trouble with for a course an assignment at uni. Really struggled with the loops in general but this bit of code has really helped. Thanks
Evan Collins
Evan Collins 2019년 10월 22일
You are a true hero thank you for your service - CUA CSC 113 Class
Syed Abdul Rafay Hammad
Syed Abdul Rafay Hammad 2022년 3월 18일
You are a champ man. This is the farmost simplest and fastest way. Thank you
Cholisina
Cholisina 2023년 3월 8일
I really happy to found this, thanks a lot
Mouli
Mouli 2023년 6월 12일
I have no words, how to thank you. God bless you. I was trying my code for hours and value was getting reset everytime. Thanks a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

shipakane ndhlovu
shipakane ndhlovu 2022년 11월 29일

0 개 추천

Can someone please explain to me, what is really happening I see the coding working with just using that but what does it mean

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 10월 23일

댓글:

2023년 6월 12일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by