Cannot call or index into a temporary array error message

16 visualizaciones (últimos 30 días)
Aleem Andrew
Aleem Andrew el 21 de Abr. de 2020
Editada: Liang Zhang el 16 de Jul. de 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);

Respuestas (1)

Tarunbir Gambhir
Tarunbir Gambhir el 27 de En. de 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 comentario
Liang Zhang
Liang Zhang el 16 de Jul. de 2023
Editada: Liang Zhang el 16 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by