开发者

MATLAB is changing variables when I do not want it to

开发者 https://www.devze.com 2022-12-28 12:00 出处:网络
Here is my problem in a small bit of code used as an example: function [] = trial(test) disp(test) if(test == 1)

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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号