Reputation: 4095
I want to initialize an array of particles where Particle is a class I defined. I'm told here that I should do it like that
particles(2, 1) = Particle();
but this doesn't work if particles
has been previously set to something like an array of doubles, or may be larger than the intended size (and won't shrink with this line, only the affected row will be changed I think). Is there a way to unset the variable to initialize my array fresh?
The error I get:
The following error occurred converting from Particle to double: Conversion to double from Particle is not possible.
Error in main (line 4) particles(2, 1) = Particle();
Upvotes: 0
Views: 34
Reputation: 60444
There are various possible solutions. To clear a variable, use clear
:
clear particles
particles(2, 1) = Particle;
(Note it's not necessary to add the empty parenthesis to call a function without arguments.)
However, a better solution IMO is to create an array of your class and assign it to the variable
particles = repmat(Particle,2,1);
This statement only works if you haven't overloaded the concatenation operator.
Upvotes: 1