필터 지우기
필터 지우기

How to Save Values to a Variable Passing through a Loop

조회 수: 3 (최근 30일)
Deborah
Deborah 2024년 1월 5일
댓글: Dyuman Joshi 2024년 1월 5일
Hello,
I am trying to store values that are filtered through a loop to the variable 'LargeBunny'. This will be a X by 3 dimension matrix with depth, Xpos, and Ypos as column values. Ultimately, I want to save all values that pass through the filter in this matrix for future reference. How do I save the Xpos and Ypos to remember each value? For example, if I want to call the 5th row of the variable outside of the loop then the Xpos and Ypos of the 5th row can be called by 'LargeBunny(5,:)'
The code as written will only report the last value in the matrix.
Depth = zeros();
LargeBunny= zeros(numel(Depth),3);
for ii = 1:numel(Size(:,1))
if Size(ii) > 1
for k = 1:numel(Xpos(:,1))
end
for m = 1:numel(Ypos(:,1))
end
fprintf(id,'G0 X%.3f Y%.3f \n',Xpos(ii,:)',Ypos(ii,:)'); %go to XY position
LargeBunny = [Depth Xpos(ii,:)' Ypos(ii,:)']; %go to XY position
end
end
  댓글 수: 3
Deborah
Deborah 2024년 1월 5일
Size is just an integer column of numbers less than or greater than 1. All I am doing is selecting which data to apply the matrix selection to. I am sitll not sure how to save the values that pass through the loop to a new array.
Dyuman Joshi
Dyuman Joshi 2024년 1월 5일
It is still not clear to me what you want to do.
What is the purpose of the loop and how are the loops related to the values you want to store?

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

답변 (1개)

Hassaan
Hassaan 2024년 1월 5일
% Assuming Depth, Xpos, and Ypos are defined elsewhere
% Initialize LargeBunny with zero rows and three columns
LargeBunny = zeros(0, 3);
% Loop through your data
for ii = 1:numel(Size) % Make sure 'Size' is defined in your workspace
if Size(ii) > 1 % Apply your filter condition here
% Assuming you have defined Xpos and Ypos corresponding to ii
% Append a new row to LargeBunny
LargeBunny = [LargeBunny; Depth(ii), Xpos(ii), Ypos(ii)];
end
end
% Now LargeBunny has all the rows that passed through the filter.
% You can access the 5th row using LargeBunny(5,:) if it exists.
Make sure Depth, Xpos, and Ypos are vectors that have the same length as Size, and that each ii iteration corresponds to the related Depth, Xpos, and Ypos values.
Note: The inner loops for k and m in your provided code don't seem to serve any purpose as they stand. If these are meant to process Xpos and Ypos further, then ensure that you include relevant operations inside these loops. Otherwise, you can remove them.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by