0

How can I change the value of 'StartRow' and 'EndRow' inside the loop?

for k = 1:11
filename = 'file.txt';   
...

startRow = 1422; %1564, 1706, 1848, 1990, 2132, 2274, 2416, 2558, 2700, 2842 
endRow = 1562; %1704, 1846, 1988, 2130, 2272, 2414, 2556, 2698, 2840, 2982
... 

f=figure;
plot(...);

saveas(f,sprintf('fig%d.png',k));

end

3 Answers 3

1

You can store all values of startRow and endRow in a list right before the for loop and then iterate through the list to change the value of these variables inside the loop.

startRowList = [1422, 1564, 1706, 1848, 1990, 2132, 2274, 2416, 2558, 2700, 2842];
endRowList = [1562, 1704, 1846, 1988, 2130, 2272, 2414, 2556, 2698, 2840, 2982];

for k = 1:11
    filename = 'file.txt';   
    ...

    startRow = startRowList(k);
    endRow = endRowList(k);
    ... 

    f=figure;
    plot(...);

    saveas(f,sprintf('fig%d.png',k));
end
Sign up to request clarification or add additional context in comments.

Comments

1

Keep startRow and endRow outside the loop and call them inside the loop using loop index.

startRow = [1422 1564, 1706, 1848, 1990, 2132, 2274, 2416, 2558, 2700, 2842] ;
endRow = [1562 1704, 1846, 1988, 2130, 2272, 2414, 2556, 2698, 2840, 2982];
n = length(startRow) ;
for k = 1:n
    filename = 'file.txt';
    thestart = startRow(k) ;
    theend = endRow(k) ;
    ...

...

f=figure;
plot(...);

saveas(f,sprintf('fig%d.png',k));
end

Comments

1
starts = [1422, 1564, 1706, 1848, 1990, 2132, 2274, 2416, 2558, 2700, 2842];
ends = [1562, 1704, 1846, 1988, 2130, 2272, 2414, 2556, 2698, 2840, 2982];

for k = 1:11
    ...
    startRow = starts(k);
    endRow = ends(k);
    ...
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.