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!
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!