5

I can't figure out how to do such a simple thing like defining constants using other ones.

For instance, a dummy example :

classdef DummyClass < handle
    properties (Constant)
        NB_SECONDS_IN_MINUTE = 60;
        NB_MINUTES_IN_HOUR   = 60;

        NB_SECONDS_IN_HOUR   = NB_SECONDS_IN_MINUTE * NB_MINUTES_IN_HOUR;
    end
end

This does not work :(

I then tried with this line :

NB_SECONDS_IN_HOUR   = DummyClass.NB_SECONDS_IN_MINUTE * DummyClass.NB_MINUTES_IN_HOUR;

but that doesn't work either...

Someone got a clue here ? :/

(I'm using MATLAB R2009a btw)

1
  • it said "undefined variable or class DummyClass". btw, Edric gave me the solution : i just forgot that my class was in a subfolder, and in Matlab, one needs to use the fully qualified name of a class (including sub-directories) Commented Jan 26, 2012 at 16:31

1 Answer 1

7

You definitely need to refer to the constants with the full class name, as in your second case. Is DummyClass within a package (+packagename) directory? If so, you need to use the fully qualified name, i.e.

NB_SECONDS_IN_HOUR = packagename.DummyClass.NB_SECONDS_IN_MINUTE * packagename.DummyClass.NB_SECONDS_IN_HOUR;

EDIT: just tested this in R2009a:

>> ver matlab
-------------------------------------------------------------------------------------
[...]
-------------------------------------------------------------------------------------
MATLAB                                                Version 7.8        (R2009a)
>> type DummyClass

classdef DummyClass < handle
    properties (Constant)
        NB_SECONDS_IN_MINUTE = 60;
        NB_MINUTES_IN_HOUR   = 60;

        NB_SECONDS_IN_HOUR   = DummyClass.NB_SECONDS_IN_MINUTE * DummyClass.NB_MINUTES_IN_HOUR;
    end
end

>> DummyClass.NB_SECONDS_IN_HOUR
ans =
        3600
Sign up to request clarification or add additional context in comments.

2 Comments

So it depends on the path where we code the class??? (and so we can't reuse this class directly in anoter project without the same directories structure?)
It only matters if you place DummyClass.m in a MATLAB package directory, i.e. one beginning with "+". When you do that, you alter the fully-qualified name of the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.