MATLAB Answers

List all variables in a mfile

조회 수: 23(최근 30일)
K E
K E 2012년 7월 20일
Is there a way to list all the variables used by a mfile? Here is the scenario: I am editing a legacy function which takes in some input structures and then expands the structure fields into variables within the function workspace. But, I don't have an example of the input structures, so I will have to recreate them, which means I must first identify what their fields and hence the variables in the function are. Because I don't know the input fields, the function won't run, so I can't use the profiler or other tool that requires a working function to identify the variables. If the whos command permitted a mfile as the 'location', that would be perfect...
  댓글 수: 4
K E
K E 2012년 7월 20일
The author doesn't have the info (code is old), so I will just fish the variables out of the code lines. If the code was a Simulink mdl instead of a mfile, I could locate the variables using findVars - which exists precisely because the default way to run Simulink is to poof variables into/out of the workspace!

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2012년 7월 20일
편집: per isakson 2012년 7월 20일
I think it is possible to do automatically. Idea:
  • set a conditional break point at the last line of every function. Hopefully there is the word, "end"
  • the "condition expression" should always return false
  • side effect: run evalin( 'caller', 'whos' ) - something like that - assignin(?)
  • no new code in the system under study is required
I've done something similar in tracer4m.
Matlab PUBLISH uses side effects of conditional break points.
  댓글 수: 2
Jan
Jan 2012년 7월 23일
A good idea. For really clean code this will work sufficiently. But unfortunately the question sounds, like the code is not programmed in s a strict style with respect to reliability. E.g. something like this will let your method fail:
if rand > 0.5, disp = 13, end
k = @fcn, if rand > 0.5, k = 17; end
Any clear command will cause troubles also. So finally after the automatic analysis, the manual control of each line is still required.
PS. I've reviewed a function today, which would let evalin('caller', 'whos') fail, because whos was a local variable. :-(
However, this is the most promissing automatic approach, and less depressed than my "better re-program everything".

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Jan
Jan 2012년 7월 20일
편집: Jan 2012년 7월 20일
Taking an input struct and assigning the fields to local variables dynamically is obviously a really bad idea. I cannot image a better way to confuse users and the Matlab interpreter, MLint and any automatic code analysis - except sorting the charatcers of the program alphabetically and storing the sorting order run-length-encoded instead of the clear source code.
Ask the author and call him names.
There is no secure way to distinguish local variables and built-in functions:
S.disp = 100;
myCruelFunc(S);
function myCruelFunc(S)
assignAsVariables(S);
disp(1) % <-- What do you expect here?!
function assignAsVariables(S)
field = fieldnames(S);
for i = 1:numel(fields)
assignin('caller', field{i}, S.(field{i}));
end
In some Matlab releases the output of "disp(1)" even denpends on the current debug status: With a breakpoint "100" is replied (disp is a variable), without any breakpoints you get "1" (disp is a function).
Therefore expanding the structure fields into variables within the function workspace is a bad idea. I'm afraid an exhaustive analysis of the function will need more time than reprogramming it in a clean way. Simply use the fields of the struct, or expand the struct explicitely:
...
disp = S.disp;
Then MLint, code-analysis and efficient processing is possible.
  댓글 수: 3
K E
K E 2012년 7월 20일
Well put. But I have to do this just often enough that I may automate it eventually.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 7월 20일
I don't think so, or else fdep ( http://www.mathworks.com/matlabcentral/fileexchange/17291) would have listed them. Why can't you just look at the source code? You can search for all "." to find structure references. Then just make a note of all the members that each structure uses. Shouldn't take too long to do it manually like that.
  댓글 수: 2
Image Analyst
Image Analyst 2012년 7월 20일
Not sure what you meant when you said "this" but the manual method I recommended is possible.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by