to find variables in a string

6 ビュー (過去 30 日間)
oblivious
oblivious 2012 年 5 月 20 日
コメント済み: Felix Mardt 2020 年 11 月 20 日
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

採用された回答

Richard Zapor
Richard Zapor 2012 年 6 月 1 日
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 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 1 日
Small adjustment: underscore should also be in the [a-zA-Z0-9] part.
Felix Mardt
Felix Mardt 2020 年 11 月 20 日
Another small adjustment: single letter variables will not be returned. I am using
[a-zA-Z]([a-zA-Z0-9_]+)?
to cover them

サインインしてコメントする。

その他の回答 (2 件)

per isakson
per isakson 2012 年 6 月 1 日
See FEX contribution: mparser
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 1 日
Interesting! I had never encountered that one!

サインインしてコメントする。


Walter Roberson
Walter Roberson 2012 年 5 月 20 日
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.)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by