Very basic object oriented question
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Kevin Bachovchin
      
 el 12 de Feb. de 2014
  
    
    
    
    
    Editada: per isakson
      
      
 el 13 de Feb. de 2014
            Hello,
I'm trying to call a method of a class in order to calculate one of the properties of the class, but I'm getting an error.
 classdef Module
    properties
        Parameters;
        StateVariables;
        ControllableInputs;
        StateSpace;
    end
    methods
        function this =Module(Parameters,StateVariables,ControllableInputs)
            % class constructor
            if(nargin > 0)
                this.Parameters = Parameters;
                this.StateVariables  = StateVariables;
                this.ControllableInputs = ControllableInputs;
                this.StateSpace = FindStateSpace(Parameters,StateVariables,ControllableInputs);
            end
        end
       function StateSpace =   FindStateSpace(Parameters,StateVariables,ControllableInputs)
             ....
        end
    end
 end
This is producing an error "Undefined function 'FindStateSpace' for input arguments of type 'sym'." Can someone please tell me what I'm doing incorrectly, and how to fix this problem? I think this is probably a very very basic question, but I'm a novice with MATLAB object oriented programming.
Thank you, Kevin
0 comentarios
Respuesta aceptada
  Eric
      
 el 12 de Feb. de 2014
        
      Editada: Eric
      
 el 12 de Feb. de 2014
  
      I don't believe you're allowed to call non-static class methods from within the constructor itself. Until the constructor returns, the variable "this" is not known to be of class Module.
Since you're passing in Parameters, StateVariables, and ControllableInputs as inputs to the FindStateSpace method, I think an easy work around is to make it a static method.
Before the final "end" statement of this file, do something like:
methods(Static)
  function StateSpace = FindStateSpace(Parameters, StateVariables, ControllableInputs)
    ....
    end
end
In the constructor, use the static method as follows:
this.StateSpace = Module.FindStateSpace(Parameters,StateVariables,ControllableInputs);
Good luck,
Eric
Más respuestas (1)
  per isakson
      
      
 el 12 de Feb. de 2014
        
      Editada: per isakson
      
      
 el 13 de Feb. de 2014
  
      Try this:
    >> mm = my_class
    mm = 
      my_class with properties:
        prop: 17
where
    classdef  my_class
        properties  
            prop
        end
        methods
            function this = my_class(  )
                this.prop = this.my_method();
            end
            function val = my_method( this )
               val = 17; 
            end
        end
    end
.
AFAIK: You can /should not have a property and a method with the same name, StateSpace
.
And static
    mm = 
      my_class with properties:
        prop: 17
where
    classdef  my_class
        properties  
            prop
        end
        methods
            function this = my_class(  )
                this.prop = my_class.my_method();
            end
        end
        methods( Static = true )
            function val = my_method()
               val = 17; 
            end
        end
    end
0 comentarios
Ver también
Categorías
				Más información sobre Properties en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


