For loop to iterate through variable

3 Ansichten (letzte 30 Tage)
Aakash Soni
Aakash Soni am 20 Mai 2021
Kommentiert: Aakash Soni am 20 Mai 2021
I have a following piece of code and need to convert it using for loop.
[lnCorrectSub2Vid1] = lineCorrection(bScrnSub2_vid1.ecg, vid1{1,2}.ecg);
[lnCorrectSub2Vid2] = lineCorrection(bScrnSub2_vid2.ecg, vid2{1,2}.ecg);
[lnCorrectSub2Vid3] = lineCorrection(bScrnSub2_vid3.ecg, vid3{1,2}.ecg);
[lnCorrectSub2Vid4] = lineCorrection(bScrnSub2_vid4.ecg, vid4{1,2}.ecg);
[lnCorrectSub2Vid5] = lineCorrection(bScrnSub2_vid5.ecg, vid5{1,2}.ecg);
[lnCorrectSub2Vid6] = lineCorrection(bScrnSub2_vid6.ecg, vid6{1,2}.ecg);
[lnCorrectSub2Vid7] = lineCorrection(bScrnSub2_vid7.ecg, vid7{1,2}.ecg);
[lnCorrectSub2Vid8] = lineCorrection(bScrnSub2_vid8.ecg, vid8{1,2}.ecg);
I wrote following code using for loop (I want do something like this) but I do not know how to iterate through variable.
for jj = 1:8
bslnCorrectSub2Vid(jj) = baselineCorrection(bScrnSub2_vid(jj).ecg, vid(jj){1,2}.ecg);
end
Can I iterate through variable or should I change variable naming?

Akzeptierte Antwort

Rik
Rik am 20 Mai 2021
You painted yourself in a corner by storing data in a variable name.
If you had used a cell array from the start, you could have used easy indexing.
The solution is to change how your data is created. Make sure not to rely on a variable name. Your entire code should still make sense if you rename a variable.
  1 Kommentar
Aakash Soni
Aakash Soni am 20 Mai 2021
Hi @Rik, Thank you for your answer. I am using cell array now. It is solved now.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by