Jimbo_ai
Jimbo_ai

Reputation: 167

MATLAB error: Input arguments to function include colon operator. To input the colon character, use ':' instead

I face the above mentioned(on title) error. Firstly, I faced a "Cannot call or index to temporary array" error. Then I fixed it(I think so, at least) and a new error occurred. Initially, I had wrote this:

Y = eye(num_labels)(y,:);

Here the "Cannot call or index to temporary array" error occurred. I changed my code to:

Y = subsref(eye(num_labels),struct('type','()','subs',{{y,:}}));

Now, I had to solve an "unexpected MATLAB operator" error on the column where the colon operator (':') lies.

I decided to change again my code to this:

 paren = @(x, varargin) x(varargin{:});
 curly = @(x, varargin) x{varargin{:}};
 Y = paren(eye(num_labels),y,:);

Now I come up with the "Input arguments to function include colon operator. To input the colon character, use ':' instead." error.

What do I have to do? Which of the above approach is right(if any)?

Thank you in advance!

Upvotes: 2

Views: 2861

Answers (1)

Daniel
Daniel

Reputation: 36710

When using subsref you can not use the colon operator, you have to pass the string ':' and it will be evaluated to the colon operator by subsref:

Y = subsref(eye(num_labels),struct('type','()','subs',{{y,':'}}));

Unless you are forced to a single line solution, use two lines and a temporary variables:

Y = eye(num_labels)
Y = Y(y,:)

In case that`s your real code and not just a simplified example, you can also use:

Y=[1:num_labels==y]

Upvotes: 3

Related Questions