How to Save Values to a Variable Passing through a Loop
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Dyuman Joshi
le 5 Jan 2024
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?
Réponses (1)
Hassaan
le 5 Jan 2024
% 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
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!