Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Go Back an index

Subject: Go Back an index

From: Jamie Macdonald

Date: 5 Jan, 2011 19:01:05

Message: 1 of 2

Hello, I want to be able to go back an index if the user inputs to go back.

This is the for loop

for i = 1:n
    disp(' ')
    shape_number = (input('enter Shape number: '));
    if shape_number == 0
        disp('Correction to last shape: ')
        i = i - 1; %%%%%%%%%%%% FIX %%% This is the index change
    else
    
    [s_{i},height,width] = shapes_((shape_number),Board);
    for j = 1:length(s_{i})
        for k = 1:vertical_dimension
            for l = 1:horizontal_dimension
                s{i}{j}(k,l) = uint8(s_{i}{j}(k,l));
            end
        end
    end
    p{i} = Goal;
    disp(s_{i}{1})
    disp(['height = ', num2str(height)])
    disp(['width = ', num2str(width)])
    disp('If this shape is wrong, type "0"')
    end
        
end

MATLAB says as a yellow warning "Loop index 'i' is changed within a FOR loop" but i doesn't change.
what am i missing?

Subject: Go Back an index

From: Sean de

Date: 5 Jan, 2011 19:14:05

Message: 2 of 2

"Jamie Macdonald" wrote in message <ig2f5g$bu4$1@fred.mathworks.com>...
> Hello, I want to be able to go back an index if the user inputs to go back.
>
> This is the for loop
>
> for i = 1:n
> disp(' ')
> shape_number = (input('enter Shape number: '));
> if shape_number == 0
> disp('Correction to last shape: ')
> i = i - 1; %%%%%%%%%%%% FIX %%% This is the index change
> else
>
> [s_{i},height,width] = shapes_((shape_number),Board);
> for j = 1:length(s_{i})
> for k = 1:vertical_dimension
> for l = 1:horizontal_dimension
> s{i}{j}(k,l) = uint8(s_{i}{j}(k,l));
> end
> end
> end
> p{i} = Goal;
> disp(s_{i}{1})
> disp(['height = ', num2str(height)])
> disp(['width = ', num2str(width)])
> disp('If this shape is wrong, type "0"')
> end
>
> end
>
> MATLAB says as a yellow warning "Loop index 'i' is changed within a FOR loop" but i doesn't change.
> what am i missing?

Use a WHILE-loop instead of a FOR-loop. WHILE-loops allow for index jumping.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us