4

I have a function that returns a handle to an instantiated object. Something like:

function handle = GetHandle()
    handle = SomeHandleClass();
end

I would like to be able to use the return value like I would if I was writing a program in C:

foo = GetHandle().property;

However, I get an error from MATLAB when it tries to parse that:

??? Undefined variable "GetHandle" or class "GetHandle".

The only way I can get this to work without an error is to use a temporary variable as an intermediate step:

handle = GetHandle();
foo = handle.property;

Is there a simple and elegant solution to this, or is this simply impossible with MATLAB's syntax?

Amro
  • 123,847
  • 25
  • 243
  • 454
Ryan Edwards
  • 633
  • 1
  • 10
  • 28
  • 2
    This is very similar to [another question asked](http://stackoverflow.com/questions/3627107/), but in reference to indexing a returned vector. – Ryan Edwards Oct 18 '11 at 20:01
  • Where does `GetBackend` come into it? – Nzbuu Oct 18 '11 at 21:08
  • The software has a "backend" class that instantiates a number of child objects. Each child object accesses methods in other children by referencing through the backend. This requires every child to be given a copy of the backend handle. I'm looking for an alternative way that would avoid needing the handle to be passed all over. My thought was to create a GetHandle() function available on the path that would return the handle. Any other suggestions on how to do this are also appreciated. – Ryan Edwards Oct 18 '11 at 22:36
  • I just realized what you meant, @nzbuu. The error was copied from my real code's error message and I forgot to change it to reflect the name I used in my example code. :) – Ryan Edwards Oct 19 '11 at 04:52

3 Answers3

2

To define static properties, you can use the CONSTANT keyword (thanks, @Nzbuu)

Here's one example from MathWorks (with some errors fixed):

classdef NamedConst
   properties (Constant)
      R = pi/180;
      D = 1/NamedConst.R;
      AccCode = '0145968740001110202NPQ';
      RN = rand(5);
   end
end

Constant properties are accessed as className.propertyName, e.g. NamedConst.R. The values of the properties are set whenever the class is loaded for the first time (after the start of Matlab, or after clear classes). Thus, NamedConst.RN will remain constant throughout a session as long as you don't call clear classes.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • In relation to my comment above (in response to @nzbuu), I have used this method to solve my problem. The "static property" I added was a function that returns the handle I am interested in. It's a bit sneaky but it lets my work this into the existing program design with little impact. Thank you. – Ryan Edwards Oct 18 '11 at 22:41
  • What about constant properties? – Nzbuu Oct 19 '11 at 12:12
  • @Nzbuu: D'oh! I've fixed the answer. – Jonas Oct 19 '11 at 13:03
1

Hmm, I don't like to disagree with Jonas and his 21.7k points, but I think you can do this using the hgsetget handle class instead of the normal handle class, and then using the get function.

function handle = GetHandle()
    handle = employee();
end


classdef employee < hgsetget
    properties
        Name = ''
    end
    methods
        function e = employee()
            e.Name = 'Ghaul';
        end
    end
end

Then you can use the get function to get the property:

foo = get(GetHandle,'Name')

foo =

Ghaul

EDIT: It is not excactly like C, but pretty close.

Ghaul
  • 3,340
  • 1
  • 19
  • 24
  • Don't worry, I still keep making mistakes or overlooking stuff, despite the gold badge :) – Jonas Oct 20 '11 at 21:47
1

The only way to have a static property in MATLAB is as a constant:

classdef someHandleClass < handle
    properties (Constant)
        myProperty = 3
    end
end

then someHandleClass.myProperty will return 3.

Nzbuu
  • 5,241
  • 1
  • 29
  • 51