How can I make mlint return all the variable names in my .m-file?

1 view (last 30 days)
My function 'calc_energy' gets the data table 'T' as input, calculates 'T.Energy' and then returns 'T'.
function [ T ] = calc_energy( T )
T.Energy = T.Power .* T.Time;
end
Now I want to write function
check_required_variables( T, 'calc_energy.m' )
that checks whether 'T' actually contains 'T.Power'. Therefore 'check_required_variables' needs to recognize all the variables used in 'calc_energy', since it doesn't know which variable to look for.
I've come across 'mlint', which I only see warning about undefined variables the are also declared in my code. This shows however that MATLAB is capable of recognizing variables before compiling. How can I make mlint return a list of all the variable names occuring in my .m-file?

Accepted Answer

Rik
Rik on 19 Jan 2018
Maybe they will change this in later releases, but currently m-lint can't detect possibly missing structure fields. I suspect this is because it can be tricky to see if a dot is object.method or structure.field. What you can do for structures is using the fieldsnames function to get a list of all the first level field names. A better idea is to use isfield. The latter is what I use when parsing a struct in a function.

More Answers (1)

Adam
Adam on 19 Jan 2018
You should just be able to use e.g.
ismember( 'Energy', T.Properties.VariableNames )
ismember( 'Power', T.Properties.VariableNames )
within your function and wrap an assert around them or whatever you want to do on finding out if they exist or not.

Categories

Find more on Structures 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!