tags:

views:

87

answers:

5

Here is my problem in a small bit of code used as an example:

function [] = trial(test)

disp(test)

if(test == 1)
    disp('test is one')
    test = 0;
end
disp(test)

When I execute

> trial(0)

Matlab prints out this:

0
'test is one'
0

This is not my real code, there are over 500 lines of it, but this is a section of my code where the problem has risen. I have used the search function to see if I have been incrementing any variables anywhere, and have put in over 2 hours trying to see why MATLAB is changing my variables when I don't want it to.

+1  A: 

Any chance you actually wrote this (note the single equals)?

if (test = 1)
  disp('test is one')
  test = 0;
end

That sets test to 1, passes the test, then sets it back to zero before you have any real opportunity to notice the problem.

EDIT:

Other things to look for would be global and evalin which both cause action-at-a-distance.

Ben Voigt
No I am sure I didn't write my code with just 1 equals sign, since the compiler would error and not actually cause the problem I am dealing with. Thanks though.
WVUstudent
@Ben Voigt: This example throws an error
Jonas
Sorry, not near my MatLab.
Ben Voigt
The single equals there would generate an error message.
woodchips
+4  A: 

When I copy-paste your function, I get the following output

>> trial(0)
 0

 0

In my many years using Matlab, I have found a quite a number of strange behaviours and Matlab bugs, but I have never seen Matlab change a variable just like that. Thus, my suspicion is that there really is a bug in your code.

In order to find the problem, I suggest that you set a stop in every line where test appears, so that you can check its value as you go through the function. Alternatively, you can periodically set conditional stops (set a stop normally, then right-click to enter a condition, such as test==1) so that the function halts execution when test has taken on an undesired value. This should allow you to quickly identify the place where bad stuff happens.

Once you identify the problematic code block, you can create a (relatively) small test case and post it here, in case the error has not become obvious.

EDIT

@woodchips has suggested several possible causes for your bug. My money would be on the precision problem, i.e. that you compare 0.000000001 to 0. To find such a problem, put a debug stop on every if-statement that involves test and check whether round(test)==test. If the output is false, you just replace test with round(test) in your if-statement, and you're done.

Jonas
+5  A: 

Unfortunately, MATLAB will never display the behavior that you claim for the code fragment you show, since this particular fragment will ALWAYS do the proper thing. You are doing something wrong here, but we cannot know what it is, and your code is too long for us to find the error anyway.

I would bet the answer lies in one of these common problems:

  1. An issue with global variables.
  2. A namespace conflict, where you have named a variable the same name as a function.
  3. A class problem, where a test is made comparing variables of different classes, perhaps uint8 and doubles.
  4. A precision problem, where a test for exact equality is done with floating point numbers.

In order to resolve the issue, I would strongly recommend the debugger. Track through your code, watching this variable to see when it changes.

woodchips
Oh, I didn't think of a precision problem. I bet it's that!
Jonas
I wouldn't bet on precision problem, since it's exactly opposite situation here - unequal values are tested to be equal. Unless the real code is different.
yuk
A: 

Thanks all, but here's the thing. I'm not using global variables, I have checked all my variable names to make sure they have an individual name, I am comparing numbers to numbers, and I am using either large values for my variables, 0's, or 1's. All my other if statements are working fine though. Here is a sample of code more closely related to where the problem is occurring, it might help with a solution.

function [] = trial(test)

disp(test)

if(test == 1)
disp(test)
disp('test is one')
end

if(test == 2)
disp(test)
disp('test is two')
end

if(test == 3)
disp(test)
disp('test is three')
end

if(test == 4)
disp(test)
disp('test is four')
end

disp(test)

then I execute.

trial(0)

What happens during the function is that the variable test will get a random value assigned to it so I would get

0
0
'test equals (one, two, three, or four)'
0

This does not happen ever time the function is ran however, but at random, sometimes it works the way I want it to, but other times it does the above. Like I said it is only for the if statements dealing with the variable test, and another section of my code that involves a different variable with different conditions. Just to reiterate it, I have already knocked the probable problems down. If anyone is feeling helpful enough, and doesn't feel like being a critic, I can email you, or post my code some where so you can look over it.

WVUstudent
Do you always pass the number 0 to the function, or a variable that you believe equals to 0? I'd also display `class(test)` in the if-block. What will happen if you rename the test variable to something less meaningful?
yuk
A: 

Again, thanks all, I figured out my problem, due to misplaced ends of loops and the absence of breaks, the test variable was changing values during the loop, however the question remains as to why the whole

0
'test is one'
0

happened since the value was changed in the loop it should have been

1
'test is one'
1

It is still a mystery I suppose, however its a fixed mystery.

WVUstudent

related questions