MATLAB Answers

1 - strange behaviour

Asked by bbb_bbb
on 29 Dec 2018
Latest activity Commented on by John
on 17 May 2019
filename='F:\__1\2\test files\file.bin';;
invoked from the Matlab Command Window - works,
running as a script - gives error:
"Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with any additional indexing or dot references.
Why is this so?


Sign in to comment.

1 Answer

Answer by Yair Altman on 30 Dec 2018
 Accepted Answer

With most Java methods, you can follow the method invocation parentheses with a dot, thereby chaining methods. For example: myJPanel.getComponent(0).getComponent(1).setVisible(false). However, such method chaining cannot be done with a constructor - it's valid syntax in Java, but Matlab's interpreter is not smart enough. In this case, simply separate the call into two parts:
filename='F:\__1\2\test files\file.bin';
jFile =;
success = jFile.delete;


on 16 May 2019
So, the command line parser is different than the script parser? That is surprising.
as far as I am aware, the behavior is the same in this regards in both the command window and scripts - in both cases you cannot chain methods to a Java constructor call, only to other methods.
on 17 May 2019
Nope, they are different. I just tested:
>> ls('D:\tmp')
>> filename = 'D:\temp\Figure1.JPG';
success =
Put those exact lines into a script? and get:
>> Untitled7
Error: Functions cannot be indexed using {} or . indexing.
Error in Untitled7 (line 2);
Conclusion: The parser in the command line, is different from the parser, that parses scripts... That's a bit of a surprise to me, but I'm not into the details of Matlab like you are.

Sign in to comment.