Task: write matlab code for a class MinAngle which calculates the minimum angle between two bearings (bearing A and bearing B).

The class shall store the two bearings as properties: angleA and angleB.
Both of these shall be stored as private properties.
The class shall implement a method InputA(angle, type) which sets the value of angleA.
The method shall have two input arguments: angle and type.
If type is ‘radians’ then the unit for angle is radians and the angle shall be stored in angleA.
If type is ‘degrees’ then the unit for angle is degrees and the angle shall be stored in angleA.
If type is any other value then an error should be given with the message ‘ERROR1’.
This is what I have so far:
classdef MinAngle < handle
properties (SetAccess = private)
angleA;
angleB;
end
methods
function InputA(obj,angle,type)
if (type = 'radians')
if(angle > 2*pi || angle < 0)
error('angle not within range')
end
angledeg = rad2deg(angle);
obj.angleA = angledeg;
elseif(type = 'degrees')
if(angle > 360 || angle < 0)
error('angle not within range')
end
obj.angleA = angle;
end
end
end
Need help after this as I am stuck.

 Accepted Answer

The assignment operator in MATLAB is =. The element-wise comparison operator is ==. But you should use neither of those in your if statements.
if (type = 'radians')
Use text comparison functions instead, or perhaps use a switch / case statement.
Other than this, is there something else you are trying to do that has you stuck?

7 Comments

I'm still stuck trying to fix this:
if (type = 'radians')
when I try to call the fuction, min.InputA(10,radians), I keep getting an error.
I've tried finding some relavent text comparison fuctions, but couldn't find any.
For the comparison, see the strcmp function.
But there is one problem I see and one suggestion I have for how you're calling this method.
To start, I suggest you name your class instance something other than min. That name already has a meaning in MATLAB, and while that variable exists you will not be able to call the min function due to function precedence order (that also will be relevant below.)
Second, when you run this code:
min.InputA(10,radians)
the first input that will be passed to the InputA method is the object. The second is the number 10. MATLAB will try to figure out what radians is to determine what the third input to the method should be. It looks for what radians means in this precedence order, starting with looking for a variable named radians and ending by looking on the MATLAB search path for a function named radians. Since it likely doesn't find anything named radians, it will throw an error.
You need to pass either a char vector or a string as that third input.
min.InputA(10, 'radians') % or
min.InputA(10, "radians")
This is how I've done the first part and it works perfectly:
classdef MinAngle < handle
properties (SetAccess = private)
angleA;
angleB;
end
% insert your code here.
methods
function InputA(obj,angle,type)
if strcmp(type, "radians")
obj.angleA = angle;
elseif strcmp(type, "degrees")
obj.angleA = angle;
elseif type ~= strcmp(type, "degrees") | strcmp(type, "radians")
error('ERROR1');
end
end
The second task is now, where I am now stuck again: The class shall implement a method called getA and The method shall return the value stored for angleA in radians.
It is simple enough if the user initially enters ma.InputA(20,'radians'), as I can create the following function:
function returnvalueA = getA(obj)
returnvalueA = obj.angleA
But if the user had inputted ma.InputA(20,'degrees') I can't seem to find a way to write a code which first checks whether the user type radians, or degrees. I know I'd have to use deg2rad to convert it to radians.
I've tried this, but it hasn't worked:
function returnvalueA = getA(obj)
if strcmp(InputA(type), "radians")
returnvalueA = obj.angleA;
elseif strcmp(InputA(type), "degrees")
returnvalueA = deg2rad(obj.angleA);
end
end
Consider using a trio of properties along the lines of the example in the first section on this documentation page. The DollarAmount property is stored as a value in US dollars. When you ask for your Balance the object decides (based on the value of the Currency property) whether to return it in (I'm guessing from the names) Euros, Pounds, or Dollars. [I don't know how closely those conversion factors are to reality right now.]
DollarAmount would be equivalent to your angle, stored internally using whichever units you want.
Balance is the angle your object returns to its caller.
Currency would be 'degrees' or 'radians'.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!