Cannot call or index into a temporary array error message

24 Ansichten (letzte 30 Tage)
Aleem Andrew
Aleem Andrew am 21 Apr. 2020
Bearbeitet: Liang Zhang am 16 Jul. 2023
I am trying to access the elements of a 1 by 2 variable that is defined by a function handle, p2r, but when I define a separate function handle p22r to do this I get the error message Cannot call or index into a temporary array. Is there a way I can access the elements of a 1 by 2 vector that has not yet been initialized?
r2p = @(x) [abs(x) rad2deg(angle(x))];
p2r = @(x) x(1)*exp(1i*deg2rad(x(2)));
pm = @(x,y) [x(1)*y(1) x(2)+y(2)];
pd = @(x,y) [x(1)/y(1) x(2)-y(2)];
p22r = @(x,y) p2r([x y])(:,1);

Antworten (1)

Tarunbir Gambhir
Tarunbir Gambhir am 27 Jan. 2021
MATLAB does not support syntax which directly index the function call return value, like "p2r([x y])(:,1)". It is recommended that you use temporary intermediate variables for indexing in a function declaration, rather than using anonymous function for 'p22r'.
As an alternative, however, you could use workaround with some limitations on the result. For example, if you need to return a value at the particular index (1,1) of 'p2r([x y])', you can use getfield or subsref functions as
p22r = @(x,y) getfield(p2r([x y]),{1,1});
or
p22r = @(x,y) subsref(p2r([x y]), struct('type', '()', 'subs', {{1, 1}}));
  1 Kommentar
Liang Zhang
Liang Zhang am 16 Jul. 2023
Bearbeitet: Liang Zhang am 16 Jul. 2023
This is just an unnecessary complication. I am wondering why this syntax is not supported while Octave and Scilab both support this syntax. The syntax could save life for some while.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by