Empty objects not calling get method?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I have a problem regarding object oriented programming in Matlab: If I have an empty object, i.e. myObject.empty I can call methods of it and in the method manage my output for the case of an empty object, for example returning an empty array. This makes downstream code easy to handle:
obj = myObj.empty
values = obj.doCalculations(1,2)
% Inside the doCalculation(obj, input1, input2) definition I can take care of returning an empty array in case isempty(obj)
if ~isempty(values)
% proceed ...
else
% handle empty cases
end
Now my problem starts with accessing properties. If I try to access the property of an empty object it just won't return anything, not an empty array, nothing the statement:
values = obj.propertyA
will just result in an error. Making my handling of downstream code more complicated. Of course I can use try and catch, but then I also have to check which error did exactly happen and so on.But what bugs me most is that in this case the getter
get.propertyA(obj)
Does not seem to be called. So there is no way to take care of this behaviour inside the class. I feel this is a strange behaviour that I can call the methods of an empty class but not its get method? Can anybody explain me why and what would be the best way to handle these cases? I know I can check first if the object is empty but since I have a lot of linked objects this is also non trivial:
values = objA.subObjB.subObjC.property1
Edit: In case it matters I'm working solemnly with handle derived classes.
0 Commenti
Risposta accettata
Matt Cohen
il 8 Set 2015
Hi David,
I understand you are encountering an issue when trying to access property values of an empty object.
When creating classes in MATLAB, the default value for properties is the empty array ('[]'). It sounds like your class constructor can handle different numbers of input arguments. If this is the case, then you might be using 'varargin' as the constructor input argument so that you can handle the different number of inputs. I have included a sample class below that handles no inputs and two inputs:
classdef example < handle
properties
a
b
end
methods
function obj = example(varargin)
if nargin == 2
obj.a = varargin{1};
obj.b = varargin{2};
end
end
function set.a(obj,val)
obj.a = val;
end
function out = get.a(obj)
out = obj.a;
end
function set.b(obj,val)
obj.b = val;
end
function out = get.b(obj)
out = obj.b;
end
function out = dummy_sum(obj)
out = obj.a + obj.b;
end
end
end
With this class, I can perform the following commands and get the displayed outputs:
>> obj1 = example()
obj1 =
example with properties:
a: []
b: []
>> obj2 = example(1,2)
obj2 =
example with properties:
a: 1
b: 2
>> obj1.dummy_sum()
ans =
[]
>> obj2.dummy_sum()
ans =
3
>> obj1.a
ans =
[]
>> obj2.a
ans =
1
In this example, I do not encounter the same issue you are having with accessing a property value of an empty object. One thing that might be causing some issues is the syntax you are using for creating the empty object: myObject.empty. I am not sure if this is pseudo-code or the actual syntax you are using; if this is how you are actually instantiating the empty object, it might not be handling the property initialization properly. You might want to try a similar format to the example above, using the 'varargin' input to handle multiple forms of object instantiations.
I hope this helps.
Matt Cohen
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Construct and Work with Object Arrays in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!