using clear all and getting as result false back?
3 views (last 30 days)
Show older comments
Hello, as i dont have much memory on my computer i am clearing the memory with the command "clear all" before i start my script. But when i use this command my result is false although i am using the same parameters. Heres the code:
clc;
clear all; // without "clear all" isequal is true otherwise false!
res1=func(x,y); // a mex function
res2 =func(x,y);
if ~isequal(res1, res2)
error('Not equal!');
else
disp('Equal');
end
Can someone explain me the reason pls why i m getting "false"? Many thx :)
Answers (3)
Jan
on 30 Jun 2013
Your description shows explicitly, that the reply of the Mex function func differs between the first call and following calls. clear all removes the mex (and all other functions) from the memory and the first time the function is called afterwards it is reloaded from disk. Then in the first run persistent variables are initialized and this could be local and global static variables in C as well as Matlab arrays locked by mxMakeMemoryPersistent or mxMakeArrayPersistent.
0 Comments
Shashank Prasanna
on 30 Jun 2013
Wrap the whole code in a function and remove clear all. What is the result now?
function testfunction()
res1=func(x,y); // a mex function
res2 =func(x,y);
if ~isequal(res1, res2)
error('Not equal!');
else
disp('Equal');
end
2 Comments
Shashank Prasanna
on 30 Jun 2013
At this point we may need to see the mex function to get any further. However if you take a look at clear function in the help here:
You'll notice that it clears all mex functions from memory and also persistence. Based on the way your mex is written, clear certainly seems to be affecting its behavior. Try to narrow down to see if just calling 'clear' does something else instead of 'clear all'.
Image Analyst
on 30 Jun 2013
We don't know what you're doing because you didn't give your code. That's not even 100% MATLAB code. How do I know? Well // is a comment in C whereas % is the comment symbol in MATLAB, and you used //. All I know is that a little test program I wrote (based on your pseudocode) works as expected. It prints out "equal":
function test()
clc;
clear all; % without "clear all" isequal is true otherwise false!
x=2;
y=10;
res1=func(x,y); % a mex function
res2 =func(x,y);
if ~isequal(res1, res2)
error('Not equal!');
else
disp('Equal');
end
function result = func(x, y)
result = x+y;
2 Comments
Image Analyst
on 30 Jun 2013
subtract the two numbers - what's the difference? Pretty small? See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F Maybe check that they're within a tolerance as a workaround
if abs(res-res2) < maxAllowableDifference
% For all intents and purposes, they're equal.
else
% They're too different, so they're "not equal"
end
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!