field name to string

조회 수: 1 (최근 30일)
Marcus
Marcus 2025년 11월 11일
답변: Walter Roberson 2025년 11월 11일
I have read this article: https://de.mathworks.com/help/matlab/ref/inputname.html where it is stated that dot indexing aka struct fields are not supported by the function inputname which makes this function for my usecase basically worthless. Arte there any other options to hand over a struct member and get the full name (aka a.b)?
  댓글 수: 4
dpb
dpb 2025년 11월 11일
a.b = 12;
getname1(a);
ans = 1×1 cell array
{'b'}
function getname1(x)
name=inputname(1);
fieldnames(x)
end
Some machinations like the above would be the only other alternative I would see, also. The callee would somehow have to know which of the fields is the one of interest which is essentially the equivalent.
As the doc explains (and one can observe if set a breakpoint and examine the call stack structure), when the argument is a reference to the memeber of a struct, the information simply isn't available.
Marcus
Marcus 2025년 11월 11일
OK, thanks for the background info.

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

답변 (1개)

Walter Roberson
Walter Roberson 2025년 11월 11일
When MATLAB encounters a.b in a calling sequence, then the "a" portion gets dereferenced, and the anonymous memory for "b" gets passed in. MATLAB never keeps track of where the anonymous memory came from. There is no hope for getting back the name "a.b" in this case.
As far as MATLAB is concerned, it is having to compute the field a.b, just as surely as it would have to compute the value for "A + 1" passed in.

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by