7

Is it possible to construct a parameterized class in Matlab? For example in Java I could say ArrayList<String> myList = new ArrayList<String>(). I have tried myList = java.util.ArrayList<String>(), but that just gives an error saying "unexpected parenthesis or bracket". I am really looking to use my own parameterized classes, but if I can get the syntax for this, it should be sufficient.

3 Answers 3

6

You can't instantiate a parametrized Java class in Matlab. This is because Matlab is an interpreted language. So, in your example, when you try

myList = java.util.ArrayList<String>()

This code is immediately interpreted and run by Matlab (and the Java code compiled). But because Java has Type Erasure all type information for myList is immediately lost. This means in the context of Matlab syntax type parameters make no sense -- so they are syntactically invalid.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I was afraid that that was the case, but I wanted to make sure.
So if I construct an ArrayList in matlab, is it possible to add a matlab object to it? (Every time I try to add an object, I get No method 'add' with matching signature found for class 'java.util.ArrayList'.)
5

Kurt is right, however a workaround would be to define your own java class that's not parameterized. public class MyList extends ArrayList<String> { }. Then in matlab you could write myList = MyList() and you would get almost all of the same method signatures as ArrayList<String>.

Comments

1

I am not a specialist in Matlab but I understand something in java.

  1. Generics are supported since java 5
  2. Generics are compile time feature. They are also called "erasures".

It means that

  1. Check which java is installed on your system and used by matlab. Probably it is configured to use java 1.4?
  2. Can you write you code using other IDE (not matlab), compile it their and then use in Matlab? Probably it will fix your problem.

The following link could probably help you: http://www.mathworks.com/support/solutions/en/data/1-1812J/

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.