Mosawi
Mosawi

Reputation: 197

Matlab: Creating a loop for multiple sequences

I would like to create a loop for multiple sequences i.e. from 0001 to 0100 and from 0150 to 0200 using the same for-loop in matlab. The matlab code below is obviously wrong (the part for number = 1:100; 150:200;) and is included for clarification:

 for number = 1:100; 150:200;
    s = sprintf('%04d', number);
    filename = ['E:\XRD\Enamel\r5004b_'  s '.dat'];
    startRow = 5;

    end

Upvotes: 2

Views: 186

Answers (2)

Mauvai
Mauvai

Reputation: 469

Its actually not far off: try this instead

for i = [1:100, 150:200]

By using square brackets you concatenate the two arrays into one. you may think that you can simply use

for i = 1:200

and have a conditional inside the loop, that when it detects 100 it jumps to 150 - this will not work, as unlike in c, MatLab keeps track of the loop variable separately (despite the loop variable being available in the loop - its a little confusing!)

Upvotes: 4

Jaxor24
Jaxor24

Reputation: 71

Make the ranges separately then put them into a single vector you iterate over.

R1 = 1:100

R2 = 150:250

R_All = [R1 R2]

For i = R_All

End

Upvotes: 3

Related Questions