to find variables in a string

11 views (last 30 days)
oblivious
oblivious on 20 May 2012
Commented: Felix Mardt on 20 Nov 2020
Hello,
this may be a simple problem. but i cant figure it out.
I have a string which is acquired from an .m file through fgetl function. How can i find the existence of any variable or built-in function inside that string? is there built-in function for searching variable name or built-in function name inside a string?
-obli

Accepted Answer

Richard Zapor
Richard Zapor on 1 Jun 2012
For variables use symvar(str). It will create a cell array of variable names.
Finding "built-in" functions can be performed using cellfun(@exist,cell_array_input)==5.
The 5 denotes built-in low level functions. A value of 2 is likely what you also want and these are the Toolbox functions like "filter2", "interp2" or "smooth3".
Another form to use directly on a string is of the form exist('log2','builtin').
To create the cell_array_input from a string use:
cell_array_input=regexp(str, '[a-zA-Z][a-zA-Z0-9]+','match')
This forces the first letter to be alphanumeric but allows functions like interp2 to be captured
  2 Comments
Walter Roberson
Walter Roberson on 1 Jun 2012
Small adjustment: underscore should also be in the [a-zA-Z0-9] part.
Felix Mardt
Felix Mardt on 20 Nov 2020
Another small adjustment: single letter variables will not be returned. I am using
[a-zA-Z]([a-zA-Z0-9_]+)?
to cover them

Sign in to comment.

More Answers (2)

per isakson
per isakson on 1 Jun 2012
See FEX contribution: mparser
  1 Comment
Walter Roberson
Walter Roberson on 1 Jun 2012
Interesting! I had never encountered that one!

Sign in to comment.


Walter Roberson
Walter Roberson on 20 May 2012
There is no routine that will check for built-in function names. This has been requested as an enhancement, I believe.
You can use regexp() to search for strings that look like variable names. A variable name must start with a Latin (English) letter, and each character after that must be a Latin (English) letter or a Latin (English) digit, or the character underscore, and must be at most 63 characters. Also, a variable name must not be a keyword. Keywords can be tested for using iskeyword()
What do you want your routine to do if there is a string such as "_key" (begins with an underscore and so is not a valid variable name) or "23skidoo" (begins with a digit and so is not a valid variable name) or "prêt" (contains a non-Latin letter and so is not a valid variable name) ?
You indicate that your string is from a .m file: does that mean you have to deal with quoted strings, and comments, and that you do not want to identify variable-name-like portions inside strings or comments? If that is the case, then your task becomes harder.
What is the purpose of your analysis? If you are attempting to determine whether a string is "safe" to evaluate, then security practice says that always always only accept strings that you are certain are safe: if you construct security code by rejecting patterns you know to be unsafe then you will very likely miss something that is unsafe (possibly it did not become unsafe until after you wrote your code.)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!