Trying to add fields to Class I am making

I am trying to add fields to a class that I am creating. Maybe I don't understand how this is done completely. I thought a class could be similar to a structure where you can define objects of that class that have specific fields filled out in the structure. I want my class to have two fields that I am having trouble with: numerator and denominator
here is the end part of my code where I am trying to make the class:
h.numerator=numa;
h.denominator=dena;
h.coeff = [];
h.symbol = 'x';
class(h)
h = class(h,'rational');
end
And the error message I get is: ??? Error using ==> class Cannot change the number of fields of class 'rational' without first typing 'clear classes'.
Error in ==> rational.rational at 58 h = class(h,'rational');
There is something fundamental that I do not understand. How do I create fields that include the numerator and denominator objects which I construct earlier in the constructor?

 Respuesta aceptada

Daniel Shub
Daniel Shub el 18 de Ag. de 2011
Have you tried
clear classes
The error message looks like you have loaded the class into memory and then made a change to the class definition.

4 comentarios

Stagleton
Stagleton el 19 de Ag. de 2011
yeah I tried to do that but without any luck.
Hmm, I fired up MATLAB today and it seems to be working now. I don't get it.
here is my code for the end of the constructor for my Class:
class(numa)
class(dena)
h.denominator=dena;
h.numerator=numa;
class(h.numerator)
h.coeff = [];
h.symbol = 'x';
h = class(h,'rational');
Daniel Shub
Daniel Shub el 19 de Ag. de 2011
So is it fixed or not. Sometimes clear classes fails if there are objects of the class in memory.
Stagleton
Stagleton el 19 de Ag. de 2011
It is working for now....but there must be something that I am missing and the problem may come back
Stagleton
Stagleton el 25 de Ag. de 2011
you were right

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by