2

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!

Jimbo_ai
  • 167
  • 1
  • 13
  • What are you actually trying to achieve? What is the desired output, some column of an identity matrix...? – mikkola Dec 04 '15 at 10:05
  • I think this is a duplicate of this http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it but in general this is one of the those things that you need to live with if using matlab. – percusse Dec 04 '15 at 10:08
  • 1
    Yes, temporary variables cannot be indexed. And in my opinion avoid complicated indexed variables, rather create several variables. Is this way, your program will be easier to read. Moreover, if you want to change it later it won't be a pain. – Zoltán Csáti Dec 04 '15 at 10:08
  • @ZoltánCsáti This is certainly not a complicated case. It is actually very common that your code gets complicated due to `Y = eye(5);` and then `Y=Y(3,:)` in particular if you have a few of these – percusse Dec 04 '15 at 10:35
  • This is it!!!! I tried to subindex it but nothing. The right syntax is @percusse 's way. Thank you everyone for helping me!!! – Jimbo_ai Dec 04 '15 at 10:42
  • DimitrisBouloutas , @percusse - Can one of you please post the solution as an answer so that this question appears resolved? – Dev-iL Dec 04 '15 at 11:00
  • Can anyone reproduce the `"Input arguments to function include colon operator. To input the colon character, use ':' instead."`? If I run the code it works fine on Matlab 2013a. – Daniel Dec 04 '15 at 11:30
  • @Daniel Yes, the `{{y,:}}` syntax is not valid in MATLAB. – sco1 Dec 04 '15 at 12:03
  • @excaza: The error belongs to the second code block, with the `paren` and `curly`, there I don't see any error. – Daniel Dec 04 '15 at 15:05
  • 1
    @Daniel if I copy the line into the editor it flags the colon. – sco1 Dec 04 '15 at 16:25

1 Answers1

3

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]
Daniel
  • 36,610
  • 3
  • 36
  • 69