field name to string

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 个评论

Can you give an example for what exactly you want to achieve ?
Something like
a.b = 12;
getname1(a.b);
ans = 0×0 empty char array
function getname1(x)
inputname(1)
end
and inputname(1) should give "a.b" ?
If yes: the only way I see is to pass the struct name together with the struct variable to the function in question.
a.b = 12;
name = 'a.b';
getname2(a.b,name)
name_x = 'a.b'
function getname2(x,name_x)
name_x
end
Certainly no documented ones, no.
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.
OK, thanks for the background info.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2025-11-11

0 个投票

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.

类别

帮助中心File Exchange 中查找有关 Programming Utilities 的更多信息

产品

版本

R2024b

标签

提问:

2025-11-11

Community Treasure Hunt

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

Start Hunting!

Translated by