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

Thread Subject:
problem with calling a class method

Subject: problem with calling a class method

From: Stan24

Date: 24 Nov, 2012 09:36:07

Message: 1 of 4

Hi,
How do I call a method with a parameter control handler?
The following example returns the error:
Undefined function 'setIndex' for input arguments of type 'double'.
Thanks,
Stan

classdef MyClass< handle
properties
 hpm_1; hpm_2; % popupmenu handlers
 ...
end
methods
...
function setIndex(obj, val)
 set(obj, 'Value', val);
end
function Item(obj)
  setIndex(obj.hpm_1, 1);
  setIndex(obj.hpm_2, 1);
end
end

Subject: problem with calling a class method

From: Nasser M. Abbasi

Date: 24 Nov, 2012 10:22:46

Message: 2 of 4

On 11/24/2012 3:36 AM, Stan24 wrote:
> Hi,
> How do I call a method with a parameter control handler?
> The following example returns the error:
> Undefined function 'setIndex' for input arguments of type 'double'.
> Thanks,
> Stan
>
> classdef MyClass< handle
> properties
> hpm_1; hpm_2; % popupmenu handlers
> ...
> end
> methods
> ...
> function setIndex(obj, val)
> set(obj, 'Value', val);
> end
> function Item(obj)
> setIndex(obj.hpm_1, 1);
> setIndex(obj.hpm_2, 1);
> end
> end
>


I have no used Matlab classes for a while. But you do
not even have a constructor. Also you are not initializing
the handles values inside the class (hpm_1, hpm_2).

I'd suggest adding a constructor, in there you set
these handles. Then later you can change them.

In this example, I passed h1=1, h2=2 for the
constructor. In your case, you need to use real handles
ofcourse.

You call it like this:

----------------------
clear all
h1=1; h2=2; o = MyClass(h1,h2)

o =

   MyClass handle

   Properties:
     hpm_1: 1
     hpm_2: 2

   Methods, Events, Superclasses

o.Item()
-----------------------------

The class will be:

---------------------------
classdef MyClass< handle
     properties
         hpm_1; hpm_2; % popupmenu handlers
     end
     methods
         
         CONSTRUCTOR
         function obj = MyClass(hpm_1, hpm_2)
             obj.hpm_1=hpm_1;
             obj.hpm_2=hpm_2;
         end
         function setIndex(~, theHandle , val)
             set(theHandle, 'Value', val);
         end
         function Item(obj)
             obj.setIndex(obj.hpm_1, 1);
             obj.setIndex(obj.hpm_2, 1);
         end
     end
end
---------------------------------

again, I have not used Matlab classes for long time.

--Nasser



But try this

Subject: problem with calling a class method

From: Stan24

Date: 24 Nov, 2012 13:28:08

Message: 3 of 4

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k8q75q$feb$1@speranza.aioe.org>...
> On 11/24/2012 3:36 AM, Stan24 wrote:
> > Hi,
> > How do I call a method with a parameter control handler?
> > The following example returns the error:
> > Undefined function 'setIndex' for input arguments of type 'double'.
> > Thanks,
> > Stan
> >
> > classdef MyClass< handle
> > properties
> > hpm_1; hpm_2; % popupmenu handlers
> > ...
> > end
> > methods
> > ...
> > function setIndex(obj, val)
> > set(obj, 'Value', val);
> > end
> > function Item(obj)
> > setIndex(obj.hpm_1, 1);
> > setIndex(obj.hpm_2, 1);
> > end
> > end
> >
>
>
> I have no used Matlab classes for a while. But you do
> not even have a constructor. Also you are not initializing
> the handles values inside the class (hpm_1, hpm_2).
>
> I'd suggest adding a constructor, in there you set
> these handles. Then later you can change them.
>
> In this example, I passed h1=1, h2=2 for the
> constructor. In your case, you need to use real handles
> ofcourse.
>
> You call it like this:
>
> ----------------------
> clear all
> h1=1; h2=2; o = MyClass(h1,h2)
>
> o =
>
> MyClass handle
>
> Properties:
> hpm_1: 1
> hpm_2: 2
>
> Methods, Events, Superclasses
>
> o.Item()
> -----------------------------
>
> The class will be:
>
> ---------------------------
> classdef MyClass< handle
> properties
> hpm_1; hpm_2; % popupmenu handlers
> end
> methods
>
> CONSTRUCTOR
> function obj = MyClass(hpm_1, hpm_2)
> obj.hpm_1=hpm_1;
> obj.hpm_2=hpm_2;
> end
> function setIndex(~, theHandle , val)
> set(theHandle, 'Value', val);
> end
> function Item(obj)
> obj.setIndex(obj.hpm_1, 1);
> obj.setIndex(obj.hpm_2, 1);
> end
> end
> end
> ---------------------------------
>
> again, I have not used Matlab classes for long time.
>
> --Nasser
>
>
>
> But try this
>

Thanks a lot. Your solution is very good.

 function setIndex(~, theHandle , val)
...
end

Subject: problem with calling a class method

From: Steven_Lord

Date: 27 Nov, 2012 17:23:17

Message: 4 of 4



"Stan24 " <ssypniew@p.lodz.pl> wrote in message
news:k8q4e6$pqc$1@newscl01ah.mathworks.com...
> Hi,
> How do I call a method with a parameter control handler?
> The following example returns the error:
> Undefined function 'setIndex' for input arguments of type 'double'.
> Thanks,
> Stan
>
> classdef MyClass< handle
> properties
> hpm_1; hpm_2; % popupmenu handlers ...
> end
> methods
> ...
> function setIndex(obj, val)
> set(obj, 'Value', val);
> end
> function Item(obj)
> setIndex(obj.hpm_1, 1);
> setIndex(obj.hpm_2, 1);

That's correct. The setIndex method for MyClass objects is only called if
you call setIndex with a MyClass object as one of its inputs. You're not
doing so. If you intend your setIndex method to set the Value property of
one of the Handle Graphics objects whose handles are stored in the MyClass
object, you would need to do something like:

classdef MyClass< handle
properties
    vectorOfPopupmenus; % popupmenu handlers
end
methods
    function setIndex(obj, whichPopupmenu, newvalue)
        set(obj.vectorOfPopupmenus(whichPopupmenu), 'Value', newvalue);
    end
    function Item(obj)
        setIndex(obj, 1, 1);
        setIndex(obj, 2, 1);
    end
end

This scales better if you have 10, 100, etc. popupmenus managed by your
object. You could also use a SWITCH/CASE in your setIndex method if you want
each popupmenu to be stored in a separate property.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us