Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a constant using another one in a Matlab class

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)

like image 961
adrien.pain Avatar asked Jan 26 '12 15:01

adrien.pain


People also ask

How do you specify constants in MATLAB?

You can define constants that you can refer to by name by creating a MATLAB® class that defines constant properties. Use constant properties to define constant values that you can access by name. Create a class with constant properties by declaring the Constant attribute in the property blocks.

Can you create a constant in MATLAB?

Accepted AnswerIt is possible to create named constants as part of a class or package in MATLAB versions 7.6 (R2008a) and above.

How do you declare a class constant?

A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

What is a constant property?

A Constant Property is an event property that can be tracked across multiple events. Segmentation is done if multiple events are done with the same value of the event property held constant.


1 Answers

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
like image 163
Edric Avatar answered Oct 17 '22 11:10

Edric