user1661303
user1661303

Reputation: 549

function definition gets parser error (MATLAB)

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?

Upvotes: 0

Views: 4121

Answers (2)

Sam Roberts
Sam Roberts

Reputation: 24127

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.

Upvotes: 1

grantnz
grantnz

Reputation: 7423

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

Upvotes: 1

Related Questions