Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top