For loop to iterate through variable

1 view (last 30 days)
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?

Accepted Answer

Rik
Rik on 20 May 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 Comment
Aakash Soni
Aakash Soni on 20 May 2021
Hi @Rik, Thank you for your answer. I am using cell array now. It is solved now.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!