Cannot call or index into a temporary array error message

조회 수: 26 (최근 30일)
Aleem Andrew
Aleem Andrew 2020년 4월 21일
편집: Liang Zhang 2023년 7월 16일
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);

답변 (1개)

Tarunbir Gambhir
Tarunbir Gambhir 2021년 1월 27일
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
Liang Zhang
Liang Zhang 2023년 7월 16일
편집: Liang Zhang 2023년 7월 16일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by