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:
Problem using 'break'

Subject: Problem using 'break'

From: Nic Roberts

Date: 27 Feb, 2012 14:49:11

Message: 1 of 3

Hi,

For some reason MALTLAB wont let me use 'break' to get out of a 'for' loop, below is the snippet of code which I keep getting the following error...

"Expression or statement is incomplete or incorrect." - refering to the line with the 'break' statement

for i=1:image_dim
    if (stack_coord(1,i)<image_dim)
        if stack(stack_coord(1,i),stack_coord(2,i),stack_coord(3,i))==255
            pix_count(div_count)=pix_count(div_count)+1;
         else
             break
           end
       end
end

Shouldnt this code pop me out of the 'for' lopp as soon as I get a value from 'stack' that isnt 255?

Subject: Problem using 'break'

From: dpb

Date: 27 Feb, 2012 17:16:11

Message: 2 of 3

On 2/27/2012 8:49 AM, Nic Roberts wrote:
> Hi,
>
> For some reason MALTLAB wont let me use 'break' to get out of a 'for'
> loop, below is the snippet of code which I keep getting the following
> error...
>
> "Expression or statement is incomplete or incorrect." - refering to the
> line with the 'break' statement
>
> for i=1:image_dim
> if (stack_coord(1,i)<image_dim)
> if stack(stack_coord(1,i),stack_coord(2,i),stack_coord(3,i))==255
> pix_count(div_count)=pix_count(div_count)+1;
> else
> break
> end
> end
> end
>
> Shouldnt this code pop me out of the 'for' lopp as soon as I get a value
> from 'stack' that isnt 255?

What's stack() ?
What/where is div_count?
Is this in an m-file? Wonder if there's a hidden character perhaps
that's confusing the interpreter? Agree, on the surface it looks ok
(altho I'd think perhaps you could vectorize this tho I don't know what
you're after, precisely).

BTW, it shouldn't be the problem here, but i (and j) are the imaginary
beasts of the same name; overloading them in Matlab can/will bite at
some unexpected time when do want/need complex variables. Best practice
is to not use them as index variables in Matlab despite the ubiquitous
practice in other languages.

I suggest trying to isolate down to the barest bones to uncover the
actual issue; it appears to me the problem is what _isn't_ shown or if
the hypothesis above has any merit at least what isn't visible.

--

Subject: Problem using 'break'

From: Nic Roberts

Date: 27 Feb, 2012 17:38:12

Message: 3 of 3

Hi thanks for the reply. The error turned out to be nothing to do with 'break'. It was (embarassingly) an error from a half typed line elsewehere in the code that I had forgotten about.

The indexing is a habit from C. I'll bare it in mind though thanks.

PS not having zero indexing is such a pain!

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