MATLAB: Dynamic comments on variables

commentdynamicvariable

Hi all,
Is there a way to add comments to variables dynamically? For example, in my code, if I type this variable in command window:
K>> obj.pmExpo.iter
ans =
2×1 cell array
[1]
[3]
Can I add a 'dynamic comment' when defining this variable, such that calling it gives the variable with some comments:
K>> obj.pmExpo.iter
ans =
2×1 cell array
[1]
[3]
this is a comment indicating the use of this variable, so I do not need to go back to the code to know what it does
Edit: thanks to all the replies, very helpful. I can only accept one answer but I really would like to accept all. Now I'm trying to edit all the help of methods including 2 parts: what the method does and what the variables do.

Best Answer

This sounds like a magic meta programming. I recommend to avoid such smart tricks and keep comments in the source code as usual. Bot of course it is possible:
obj.pmExpo.iter_comment = ['this is a comment indicating the use of this variable, ', ...
'so I do not need to go back to the code to know what it does'];
And now the function to display a field:
function MagicDisp(S, Field)
sprintf('Field: %s\n', Field);
disp(S.(Field));
Magic = [Field, '_comment'];
if isfield(S, Magic)
fprintf('%s\n', S.(Magic));
end
end
Now call it as:
MagicDisp(obj.pmExpo, 'iter')
Well, the name "MagicDisp" implies, that I would not use it for productive code. The function could be enhanced, but I would not do it, because variable which do explain their meaning actively are too tricky to be useful. What would happen if you run a larger code, which uses 200 of such variables and process them in a loop?
If a variable should do anything actively, that it would be starting the daily backup. This is more important, forgotten too often, and the explanations can appear in the code instead.