How to show the inputs to a function from within?
Show older comments
I'm trying to show (for debugging purposes) the input values to a function from within that function. If the inputs are provided through varargin, then a simple loop will do it:
output = myfun(varargin)
fprintf('\n%s was called with the following inputs:',mfilename)
for i = 1 : nargin
fprintf('\n\t%s',varargin{i})
end
But if the function takes required inputs, then I cannot use the loop above but have to actually name all the inputs directly:
output = myfun(x,y,z)
fprintf('\n%s was called with the following inputs:',mfilename)
fprintf('\n\t%s',x)
fprintf('\n\t%s',y)
fprintf('\n\t%s',z)
This is cumbersome if there are many inputs. Also, it doesn't allow to copy-paste the nice loop to any function I want - I have to edit it for each function depending on the number and names of the inputs.
So the question is: Is there a way to do this with a loop? Essentially I would have to collect the input-names in a cell (like varargin) and then loop over that cell. But how to do this???
Note that the function inputname() doesn't solve this - it gives the name of the workspace variable, but I don't want the variable name, I want the value.
Thanks to everyone!
Accepted Answer
More Answers (1)
Yoel Lax
on 13 May 2015
0 votes
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!