function introspection argument validation

Is it possible for a function to know the caller's name of an argument, perhaps through introspection?
My use case: a generic/specialized helper function that prints out the variable name and (formatted) value? E.g., I have a function that prints out the position and velocity of an object that I use while debugging. I'd like to print out the variable's name and its position and vel. Yes, I can pass in the variable name, but I would like to bypass one step. R2026a is fine--I know it has new introspection functionality.
Here is a sample function.
function prtPosVel( pos, vel )
if( nargin < 2 )
vel = pos(4:6);
pos = pos(1:3);
end % if state was passed in
% Get the name of the pos variable passed in.
varName = "pos"; % Get actual name
% print to screen
fprintf("%s: pos=[%.3f,%.3f,%.3f]; vel=[%.3f,%.3f,%.3f]\n", ...
varName, pos, vel);
end % function prtPosVel
Thanks!

 采纳的回答

It sounds like you're describing inputname?

1 个评论

Thanks, learnt something new today! This will have to do. From the helpfile you referenced, it can't find the name for a bunch of cases. But, that maybe the best we can do. Anyway, I accept your answer with thanks.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

产品

版本

R2026a

Community Treasure Hunt

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

Start Hunting!

Translated by