Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How to solve this issue?Help me out?

Asked by Arun Badigannavar on 8 Mar 2013
classdef CEntity
     properties
        RandNumb
     end
     methods
         function obj = CEntity
         InitArray12()
         end
     end
  end 
   function InitArray12()
         obj.RandNumb=randi(100)
   end
How to send value which is calculated in the "InitArray12" function to "RandNumb" which is there in the properties

3 Comments

Nath

Nath (view profile)

on 8 Mar 2013
classdef CEntity
     properties
        RandNumb
     end
     methods
         function obj = CEntity
         obj.RandNumb= InitArray12()
         end
     end
  end 
   function v=InitArray12()
         v=randi(100)
   end
Nath

Nath (view profile)

on 8 Mar 2013

The function InitArray12 is outside the classdef so it doesnt know the instance. Change it to return your values, and assign them in the constructor

Cedric Wannaz

Cedric Wannaz (view profile)

on 8 Mar 2013

Are you sure that you want InitArray outside of your class definition? If so, Nath answered above; otherwise, you'll want to do something like:

 classdef CEntity
    properties
       RandNumb
    end
    methods
       function obj = CEntity()
          obj = obj.InitArray12() ;
       end
       function obj = InitArray12(obj)
          obj.RandNumb = randi(100) ;
       end
    end
 end 

Products

No products are associated with this question.

1 Answer

Answer by per isakson

per isakson (view profile)

on 8 Mar 2013
Accepted answer

Or this way

    >> ce = CEntity()
    ce = 
      CEntity
      Properties:
        RandNumb: 92
      Methods
    >> 

where

    classdef CEntity < handle
        properties
            RandNumb
        end
        methods
            function obj = CEntity
                InitArray12();
            end
            function  InitArray12( obj )
                obj.RandNumb = randi( 100 );
            end
        end
    end

Read the entry "Value or Handle Class — Which to Use" in the on-line help

0 Comments

per isakson

per isakson (view profile)

Contact us