How to Save Values to a Variable Passing through a Loop

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 Comments

A couple of obscure points in the code
(1) What is Size()? Is it a variable or size of a ... ??? variable
(2) Size(ii) > 1 is not quite clear.
(3) Construction of loops with an if statement is also a bit unclear.
Just presuming some points, is this what you are trying to get:
Depth = randi(15, 10,1);
Xpos = linspace(0, 10, 10);
Ypos = linspace(0, 20, 10);
LargeBunny = [];
for ii = 1:numel(Xpos)
fprintf('G0 = %f; Xpos = %f; Ypos = %f \n', [Depth(ii), Xpos(ii), Ypos(ii)])
LargeBunny = [ LargeBunny;
Depth(ii), Xpos(ii), Ypos(ii);];
end
G0 = 12.000000; Xpos = 0.000000; Ypos = 0.000000 G0 = 15.000000; Xpos = 1.111111; Ypos = 2.222222 G0 = 12.000000; Xpos = 2.222222; Ypos = 4.444444 G0 = 12.000000; Xpos = 3.333333; Ypos = 6.666667 G0 = 6.000000; Xpos = 4.444444; Ypos = 8.888889 G0 = 11.000000; Xpos = 5.555556; Ypos = 11.111111 G0 = 14.000000; Xpos = 6.666667; Ypos = 13.333333 G0 = 5.000000; Xpos = 7.777778; Ypos = 15.555556 G0 = 10.000000; Xpos = 8.888889; Ypos = 17.777778 G0 = 8.000000; Xpos = 10.000000; Ypos = 20.000000
disp(LargeBunny)
12.0000 0 0 15.0000 1.1111 2.2222 12.0000 2.2222 4.4444 12.0000 3.3333 6.6667 6.0000 4.4444 8.8889 11.0000 5.5556 11.1111 14.0000 6.6667 13.3333 5.0000 7.7778 15.5556 10.0000 8.8889 17.7778 8.0000 10.0000 20.0000
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.
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?

Sign in to comment.

Answers (1)

% 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

Categories

Products

Release

R2015b

Asked:

on 5 Jan 2024

Commented:

on 5 Jan 2024

Community Treasure Hunt

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

Start Hunting!