0

Beginner matlab user here. I'm trying to write a function that multiplies a and b and returns the product if a and b is positive and -abs(a*b) if any of them is negative. This is what I've got.

function y = MulAnd(a,b)
%MULAND Summary of this function goes here
%   Detailed explanation goes here
if(a<0||b<0)
    y = -(abs(a*b));
else
    y = a*b;
end
end

Matlab doesn't like it. What am I doing wrong?

7
  • 3
    My MATLAB likes it. When your MATLAB doesn't like it, what does it say? Commented Sep 23, 2013 at 20:50
  • Also, how are you trying to call the function? Are you passing scalars to your function? Maybe you misunderstood the error. Commented Sep 23, 2013 at 20:57
  • At the moment, it's scalars. At a later stage I want to expand it to vectors and do the multiplication and sign testing piece-wise. Do I need to write a for-loop? Commented Sep 23, 2013 at 21:30
  • It gets red at the last "end" at it tells me "An M-Lint problem analyzing this file caused your previous MATLAB session to terminate unexpectedly". Commented Sep 23, 2013 at 21:32
  • 3
    mathworks.co.uk/support/solutions/en/data/1-74JVT3 Commented Sep 23, 2013 at 23:00

2 Answers 2

1

I think your code is fine, and I think @OlegKomarov 's comment contains the answer.

When MATLAB crashes unexpectedly due to a problem with the MATLAB Code Analyzer, it adds the name of the file that caused the problem to the file MLintFailureFiles. This causes the red indicator to appear in the MATLAB file when you later open it.

Try this:

  1. Type cd(prefdir).
  2. Open MLintFailureFiles, and remove the name of the file (MulAnd.m).
  3. Save and close MLintFailureFiles.

Now try again with MulAnd.

Sign up to request clarification or add additional context in comments.

3 Comments

But why does it crash? Did I do something wrong the first time I ran it? I just don't want to hide warnings but address the underlying problem if it is possible.
If MATLAB is repeatedly or reproducibly crashing, that would be something to look into. This would seem like a simple piece of code, so I would be surprised if that were the case, but if it does then let's work that out. But if it just happened once, there could be any number of reasons - perhaps the file got corrupted somehow, and the Code Analyzer got confused when parsing it. I would follow the above, if it doesn't happen again don't worry, but if it repeatedly does then work that out.
Thanks, it's a little bit strange that such a simple piece of code crashed right away. But it hasn't happened again, so I guess it's okay.
1

You could try the following which will work with scalars or vectors

function y = MulAnd(a,b)
%MULAND Summary of this function goes here
%   Detailed explanation goes here
y = a.*b;
negative = a<0 | b<0;
y(negative) = -abs(y(negative));
end

1 Comment

Although it works to call the function, it still gives the same warning. Something has to be wrong in my matlab setup or something if such a simple function causes it to crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.