List all variables in a mfile
Ältere Kommentare anzeigen
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 Kommentare
Sean de Wolski
am 20 Jul. 2012
And that is why we recommend against "poofing" variables...
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.
Image Analyst
am 20 Jul. 2012
I don't see anything wrong with it if it makes it more convenient. For example what's wrong with
storedStructure = load(fullMATFilename);
userSettings = storedStructure.userSettings;
(By the way, don't use settings for a variable name if you have R2012a or later - I found out the hard way that settings is now a reserved word.) MLint and the interpreter won't get confused about userSettings at all. I know they don't because I do it.
K E
am 20 Jul. 2012
Akzeptierte Antwort
Weitere Antworten (2)
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 Kommentare
Image Analyst
am 20 Jul. 2012
Seems like you're going to spend way more time on that than just manually inspecting the code. In fact, you'd be done by now if you had done that first instead of asking people for an automatic way.
K E
am 20 Jul. 2012
Image Analyst
am 20 Jul. 2012
0 Stimmen
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 Kommentare
K E
am 20 Jul. 2012
Image Analyst
am 20 Jul. 2012
Not sure what you meant when you said "this" but the manual method I recommended is possible.
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!