How to loop through a set of variables Y1..Y100
조회 수: 52 (최근 30일)
이전 댓글 표시
Hi,
I've a group of matrix named Y1 .. Y100. Each matrix is 7244x1 (complex number).
I want to loop through them (Y1..Y100) so in each iteration I can plot the 7244x1 values.
How can I iterate through them?
Thanks in advance
댓글 수: 6
Ahmed AKL
2018년 3월 1일
Sure, it will be nice if you can find a way to import the data in a better format.
The analyzer captures the RF signal and exports it to a .mat file (it doesn't give me any option to configure the format). Then I go to Matlab and load the .mat file, so I've a punch of data in my workspace as I explained in the question.
Do you have a solution for this?
채택된 답변
Stephen23
2018년 3월 1일
편집: Stephen23
2019년 6월 27일
"it will be nice if you can find a way to import the data in a better format."
Of course, it is simpler and more efficient to load into an output a structure and then access its fieldnames:
figure()
hold on
S = load(...);
For k = 1:100
plot(real(S.(sprintf('Y%d',k))))
end
So easy! By simply avoiding eval I wrote more efficient code using fewer characters: when beginners learn to avoid eval then they also write simpler, less buggy, more efficient code:
댓글 수: 0
추가 답변 (1개)
jonas
2018년 2월 28일
편집: jonas
2018년 3월 1일
Try this:
figure;hold on
for i=1:100;
plot(eval(sprintf(['Y',num2str(i)])));
end
For more info: https://se.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop
EDIT: For future reference, Stephen gave a better solution below. Only use EVAL() if it is absolutely necessary (which it rarely is, if ever?). See link for more info.
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!