Why does my Instantiate function not create a ‘Blank’ instance of That?
I have the following minimal class :
classdef That < handle
properties
This = ''
end
methods
function Self = That(String)
if exist('String','var') == 1
Self.This = String;
end
end
function Self = Instantiate(Self)
if isempty(Self)
Self(1,1) = That;
end
end
end
end
If I run
This = That;
disp(size(This)) % 1x1
disp(isempty(This)) % False
and it’s all good, I have a ‘Blank’ instance of the class
If I run
TheOther = That.empty;
disp(size(TheOther)) % 0x0
disp(isempty(TheOther)) % True
TheOther.Instantiate;
disp(size(TheOther)) % 0x0 - Expecting 1x1
disp(isempty(TheOther)) % True - Expecting False
As you can see running my Instantiate doesn’t work and I can’t see why ? Surely it should replace the empty instance with a non empty, yet blank, one ?
UPDATE :
The Link from SCFrench lead to this http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html under the heading Creating Empty Arrays, though this didn’t work either :
function Self = Instantiate(Self)
if isempty(Self)
Blank = That;
Props = properties(Blank)
for idp = 1:length(Props)
Self(1,1).(Props{idp}) = Blank.(Props{idp});
end
end
end
Maybe you should make it a static function:
Then: