I am implementing a class which must allow dynamic properties to be added and their values modified at will by the user. There is no way to tell what the name of the property will be beforehand, or how many dynamic properties will be added, so I want to use the same SetMethod for all dynamic properties.
Here's the problem: when the SetMethod is invoked, its only input arguments are the object and the value that I'm trying to set the dynamic property to - I can't find any way to pass the name of the property to the SetMethod. I've tried passing the name of the dynamic property into the SetMethod via the function handle, but Matlab complains that there are "Too many input arguments" whenever I do this.
This code demonstrates the issue:
classdef MyClass < dynamicprops
properties
end
methods
function mc = MyClass(prop_name,prop_data)
narginchk(0,2);
if nargin < 2
return;
end
p = addprop(mc,prop_name);
p.SetMethod = @(x)dataSetter(prop_name);
mc.(prop_name) = prop_data;
end
function dataSetter(mc,prop_name,prop_data)
mc.(prop_name) = prop_data;
end
end
end
With that class implemented, I get this:
>> mc = MyClass('tmp','test')
Error using MyClass>@(x)dataSetter(prop_name)
Too many input arguments.
Error in MyClass (line 20)
mc.(prop_name) = prop_data;
20 mc.(prop_name) = prop_data;
I'm using Matlab r2013b.
Thanks!!!