using clear all and getting as result false back?

1 view (last 30 days)
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
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.

Shashank Prasanna
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
mick strife
mick strife on 30 Jun 2013
Hello Shashank thx for your effort. Well i got still the same problem. may be is important that the function "func" is a mex-function? and may be thats reason? however i ve run the code in a shortcut and also as you proposed in a function. same negative result unfortunately. thx anyway :)
Shashank Prasanna
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'.

Sign in to comment.


Image Analyst
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
mick strife
mick strife on 30 Jun 2013
hello thx for your answer. well i just made the "//" comments here to make it clearer. i m not using this in my m-script code. also i have simplified the code as the full code would be to much i think...
ok thank you that u ve tried that out. currently i have no idea where i should look to find the problem. i m using matlab with mex. the functions in my script are mex-functions. may be theres the problem. if u have any hint or idea i would be grateful. i m already sitting for hours to find the problem
Image Analyst
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

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!