0

I am currently trying to use a Java package in Matlab, which works fine so far. But now I need to do a Switch case in Matlab depending on a Java enum. Here a small example code:

Java:

package abc;

class Parser {
    enum FileType {
        UNHANDLED,
        XML,
        BINARY
    }

    public getFileType(){
        this.fileType;
    }

    private FileType fileType;

}

In Matlab, I want to do the following:

parser = abc.Parser();
Switch (parser.getFileType()) {
      case abc.FileType.BINARY:
            %TODO
      break;
}

According to using Java enums or public static fields in MATLAB, I can access the enum elements with

javaMethod('valueOf', 'abc.Parser$FileType', 'BINARY');

but if I use this line in the Switch case:

parser = abc.Parser();
Switch(Parser.getFileType()){
      case javaMethod('valueOf', 'abc.Parser$FileType', 'BINARY');
            %TODO
      break;
}

I get the error

??? SWITCH expression must be a scalar or string constant.

Has anyone a work around to achieve my desired behaviour?

1 Answer 1

1

Your java doesn't compile. I changed the java to:

public class Parser {
    enum FileType {
        UNHANDLED,
        XML,
        BINARY
    }

    public FileType getFileType(){
        return this.fileType;
    }

    private FileType fileType = FileType.XML;

    public Parser(){
    }
}

For the switch I just compare the strings wrapped with char().

eg:

javaaddpath('/home/shackle/NetBeansProjects/JavaApplication50/dist/JavaApplication50.jar')
import abc.Parser

p = Parser();
switch char(p.getFileType().toString())
    case 'BINARY'
            fprintf('type is binary')
      break;

    case 'UNHANDLED'
            fprintf('type is UNHANDLED')
      break;

    case 'XML'
            fprintf('type is XML')
      break;
end
Sign up to request clarification or add additional context in comments.

Comments

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.