Matlab - Combining enumeration classes with non-static methods

Charlie

I am attempting to combine an enumeration class with non-static methods in Matlab. I wish to create a 'LogEvent' class, which has the function 'log' which takes as an input argument an enumeration member (such as LogEvent.INFO, LogEvent.ERROR, or LogEvent.WARNING) and a string, for the purpose of appending this message in a file. I wish to use this LogEvent class repeatedly, for different programs, and as such the class has the property 'fileName' which is specified during construction and refers to the output file. Below is the code forming my classdef file:

classdef LogEvent
%Class definition for logging events.

properties
    fileName;
end

methods
    function obj = LogEvent(outFile)
        obj.fileName = outFile;
    end

    function log(obj,type,logStr)

        switch (type)
            case LogEvent.INFO
                typeStr = 'INFO';
            case LogEvent.WARNING
                typeStr = 'WARNING';
            case LogEvent.ERROR
                typeStr = 'ERROR';
        end

        FID = fopen(obj.fileName,'a');
        Str = sprintf('%s - %s: %s\n',datestr(now),typeStr,logStr);
        fprintf(FID,Str);
        fclose(FID);
    end
end

enumeration
    INFO,
    WARNING,
    ERROR
end
end

Now admittedly I don't have a lot of experience programming so I may be approaching this the completely wrong way, though I have tried googling this problem but with little result - I may not know some particular keywords which would 'hit the nail on the head'. It is my belief though because multiple instances of this class need to be created (to refer to different files), the 'log' function needs to be non-static. I get this error message attempting to create an instance of this class though:

Error using LogEvent
While creating an instance of class 'LogEvent':
No value has been provided for the enumeration member named 'INFO'.  For an
enumeration derived from a built-in class, a value must be provided for each
enumeration member.

Error in ZOHB (line 10)
obj.Log = LogEvent('ZOHB.log');

Inside the 'ZOHB' class, I attempt to create an instance of the LogEvent class, and assign it as a property of the ZOHB class.

sebastian

In Matlab's enumeration scheme, the enumerated values must be instances of the class containing the enum. So e.g. WARNING would have to a certain LogEventinstance.

E.g. like in this example from the docs:

classdef Bearing < uint32
   enumeration
      North (0)
      East  (90)
      South (180)
      West  (270)
   end
end

Which means in your case, you'd have to specify arguments which would fit your LogEvent-constructor - this is what the error message says, basically. Which is of course totally nonsense in your use-case.

In your special case, you'd better make ERROR, WARNING and INFO constant properties:

properties (Constant)
    INFO = 1;
    WARNING = 2;
    ERROR = 3;
end

You can access constants in a static manner, so your remaining code should pretty much work with this version.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matlab - Combining enumeration classes with non-static methods

From Dev

pytest not allowing classes with non-static methods

From Dev

Static Classes, Methods and main()

From Dev

Non static methods vs Utilty with static methods

From Dev

Static methods vs non static methods in .Net

From Dev

Mockito with static methods in util classes

From Dev

module with classes with only static methods

From Dev

module with classes with only static methods

From Dev

Mockito with static methods in util classes

From Dev

are Classes and Methods in the Javax package Static or not?

From Dev

Working-Around Class.Method call, for non-static Classes/Methods

From Dev

Access methods with other classes in MATLAB

From Dev

Can static methods in javascript call non static

From Dev

android jni from static to non static methods

From Dev

Wrapping non-static classes in a static container

From Dev

Static or Non Static methods, thread safety is for the types not methods

From Dev

Calling non-static methods from static methods with use of "self"

From Dev

PHP static methods vs non-static methods/standard functions

From Dev

Why should 'getter' methods in 'Manager' classes not be static?

From Dev

Implementing Static Methods common to multiple Classes

From Java

What is the benefit of nesting static classes without methods?

From Dev

Use classes or modules to group static like methods?

From Dev

Code Metrics - Static classes and methods count

From Dev

Static methods in R6 classes

From Dev

Static classes, methods, and fields in GWT client

From Dev

Observer pattern and DAO classes with CRUD static methods

From Dev

Can derived classes have static get methods?

From Dev

Testing static classes and methods in C# .NET

From Dev

Static classes, methods, and fields in GWT client

Related Related

  1. 1

    Matlab - Combining enumeration classes with non-static methods

  2. 2

    pytest not allowing classes with non-static methods

  3. 3

    Static Classes, Methods and main()

  4. 4

    Non static methods vs Utilty with static methods

  5. 5

    Static methods vs non static methods in .Net

  6. 6

    Mockito with static methods in util classes

  7. 7

    module with classes with only static methods

  8. 8

    module with classes with only static methods

  9. 9

    Mockito with static methods in util classes

  10. 10

    are Classes and Methods in the Javax package Static or not?

  11. 11

    Working-Around Class.Method call, for non-static Classes/Methods

  12. 12

    Access methods with other classes in MATLAB

  13. 13

    Can static methods in javascript call non static

  14. 14

    android jni from static to non static methods

  15. 15

    Wrapping non-static classes in a static container

  16. 16

    Static or Non Static methods, thread safety is for the types not methods

  17. 17

    Calling non-static methods from static methods with use of "self"

  18. 18

    PHP static methods vs non-static methods/standard functions

  19. 19

    Why should 'getter' methods in 'Manager' classes not be static?

  20. 20

    Implementing Static Methods common to multiple Classes

  21. 21

    What is the benefit of nesting static classes without methods?

  22. 22

    Use classes or modules to group static like methods?

  23. 23

    Code Metrics - Static classes and methods count

  24. 24

    Static methods in R6 classes

  25. 25

    Static classes, methods, and fields in GWT client

  26. 26

    Observer pattern and DAO classes with CRUD static methods

  27. 27

    Can derived classes have static get methods?

  28. 28

    Testing static classes and methods in C# .NET

  29. 29

    Static classes, methods, and fields in GWT client

HotTag

Archive