That's what I use. You can use the slightly terser form that takes a scalar size argument.
r = repmat(MyClass, 0);
Note that you're not declaring the variable to have a type; it's still just the value held in the variable that has a type.
This will work with both old style and new MCOS classes. If you're using all new style classes, gnovice's "empty()" sounds like a good idea.
If you're feeling advanced, there's another option, which I'm including for completeness.
You can also handle this in subsasgn for your objects, at least for old school Matlab class. If you do an indexed assignment into an unitialized variable with a user defined object on the RHS ("right hand side"), subsagn for that class gets invoked, with the LHS coming in as [] (an empty double). If you have a special constructor form that allows you to construct an empty object without calling repmat on the object, you can support this so users do not have to preallocate their variables with objects of your class.
In your subsasgn:
function obj = subsasgn(obj, S, B)
...
s = S(1);
...
switch s.type
case '()'
% Handle dispatch on LHS autovivification
if isnumeric(obj) && isa(B, mfilename('class'))
% Must use special ctor to preallocate
obj = feval(class(B), mxdims(size(B)));
end
Then in your constructor, have a backdoor calling form for constructing empties by blessing pre-repmatted structs.
function MyClass(varargin) %constructor
SuperClasses = { }; % if you inherit, fill this in
if nargin == 1 && isa(varargin{1}, 'mxdims')
% special backdoor to support preallocation without repmat
s = repmat(DataStructure, msize(varargin{1})); % built-in repmat called on plain struct
out = class(s, mfilename, SuperClasses{:});
return;
end
...
The @mxdims class is a special class you'll need to create that holds a size vector and serves as a marker to invoke this backdoor form. The msize() method returns the size vector it represents.
If you define MyClass so it supports this, then you can just do "s(1) = MyClass" without preallocating s. You can't do "s(end+1)" though; "end" only works with preallocated values.
This is a tricky area of Matlab to be working in. Working in subsasgn and the type system like this can produce lots of subtle errors, including segfaults. Doing it this way will get you more "complete" behavior for your user defined objects. But it involves work and brittleness, and you're probably better off sticking with "repmat(class, 0)" or "empty()".