MATLAB: Adding property dynamically in the class

adding property dynamically in the classoop

I have a class,
classdef data < dynamicprops
properties
result1 = []
result2 = []
end
end
Now I need to add dynamically many properites to the class. For example
reult 3, result 4, result 5 etc
I tried using data.('result3') = [] like how we add a field in the strcture. But i am getting the error.
how can i do this?
Thanks a lot

Best Answer

A rule in any programming language: if you*re numbering variables, you're doing it wrong. These obviously related variables should all be just one variable, a container for whatever is in each of these variables. In matlab, it's matrix or cell array or table.
Assuming your results are going to be matrices of varying size, then:
classdef data < handle
properties
results = {[], []}; %two empty results in the results container
end
methods
function addresult(this, result)
this.results = [this.results, {result}];
end
function setnthresult(this, n, result)
validateattributes(n, {'numeric'}, {'integer', 'positive', '<=', numel(this.results)});
this.results{n} = result;
end
end
end
would make your life much easier.
If you really insist on using dynamic properties, then this page explains exactly how to do it, with example.