Challenge - 5 Problems
MATLAB Constructor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a simple MATLAB class constructor
What is the output of the following MATLAB code when creating an object of class
MyClass?MATLAB
classdef MyClass
properties
Value
end
methods
function obj = MyClass(val)
if nargin > 0
obj.Value = val;
else
obj.Value = 0;
end
end
end
end
obj = MyClass(5);
disp(obj.Value);Attempts:
2 left
💡 Hint
Look at how the constructor assigns the property
Value when an argument is passed.✗ Incorrect
The constructor checks if an input argument is given (nargin > 0). Since 5 is passed, it sets
Value to 5. The disp prints 5.❓ Predict Output
intermediateDefault property value with no constructor input
What will be displayed when the following MATLAB code runs?
MATLAB
classdef Sample
properties
Number = 10;
end
methods
function obj = Sample(num)
if nargin > 0
obj.Number = num;
end
end
end
end
obj = Sample();
disp(obj.Number);Attempts:
2 left
💡 Hint
Consider the default value assigned to the property and what happens if no input is given.
✗ Incorrect
Since no input is passed, the constructor does not change the default property value 10. So
disp prints 10.🔧 Debug
advancedIdentify the error in this MATLAB constructor
What error will occur when running this MATLAB class code?
MATLAB
classdef Person
properties
Name
Age
end
methods
function obj = Person(name, age)
obj.Name = name;
obj.Age = age;
end
end
end
p = Person('Alice');Attempts:
2 left
💡 Hint
Check how many arguments the constructor expects versus how many are given.
✗ Incorrect
The constructor expects two inputs but only one is provided, causing a 'Not enough input arguments' error.
🧠 Conceptual
advancedBehavior of handle class constructor in MATLAB
Consider this MATLAB class that inherits from
handle. What will be the output of the code below?MATLAB
classdef Counter < handle
properties
Count = 0;
end
methods
function obj = Counter(start)
if nargin > 0
obj.Count = start;
end
end
function increment(obj)
obj.Count = obj.Count + 1;
end
end
end
c1 = Counter(5);
c2 = c1;
c2.increment();
disp(c1.Count);Attempts:
2 left
💡 Hint
Remember that handle classes share the same object reference.
✗ Incorrect
Since
Counter inherits from handle, c1 and c2 point to the same object. Incrementing c2 changes c1.Count to 6.❓ Predict Output
expertOutput of constructor with varargin and property assignment
What is the output of this MATLAB code when creating an object with
obj = Config('Mode','Auto','Level',3);?MATLAB
classdef Config
properties
Mode = 'Manual';
Level = 1;
end
methods
function obj = Config(varargin)
for k = 1:2:length(varargin)
prop = varargin{k};
val = varargin{k+1};
if isprop(obj, prop)
obj.(prop) = val;
end
end
end
end
end
obj = Config('Mode','Auto','Level',3);
disp([obj.Mode, ' ', num2str(obj.Level)]);Attempts:
2 left
💡 Hint
Look at how the constructor uses
varargin to assign properties dynamically.✗ Incorrect
The constructor loops through pairs of property names and values, updating
Mode to 'Auto' and Level to 3. The disp prints 'Auto 3'.