Filter löschen
Filter löschen

How to loop through a set of variables Y1..Y100

52 Ansichten (letzte 30 Tage)
Ahmed AKL
Ahmed AKL am 28 Feb. 2018
Bearbeitet: Stephen23 am 27 Jun. 2019
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 Kommentare
Ahmed AKL
Ahmed AKL am 1 Mär. 2018
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
Stephen23 am 1 Mär. 2018
Bearbeitet: Stephen23 am 1 Mär. 2018
"Do you have a solution for this?"
Yes, as I already wrote, just load the data into a variable. This was also explained in the tutorial that I gave you a link to: did you read it? See my answer for how this can be applied to your situation.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 1 Mär. 2018
Bearbeitet: Stephen23 am 27 Jun. 2019
"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:

Weitere Antworten (1)

jonas
jonas am 28 Feb. 2018
Bearbeitet: jonas am 1 Mär. 2018
Try this:
figure;hold on
for i=1:100;
plot(eval(sprintf(['Y',num2str(i)])));
end
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.
  2 Kommentare
Ahmed AKL
Ahmed AKL am 28 Feb. 2018
Thanks a lot.
It works fine. but I made a small change to plot the real value without the imaginary part
plot(real ( eval(sprintf(['Y',num2str(i)]))) );
Stephen23
Stephen23 am 1 Mär. 2018
Bearbeitet: Stephen23 am 1 Mär. 2018
Even better would be to import the data properly so that slow and buggy eval is not required.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by