Borrar filtros
Borrar filtros

to find variables in a string

3 visualizaciones (últimos 30 días)
oblivious
oblivious el 20 de Mayo de 2012
Comentada: Felix Mardt el 20 de Nov. de 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

Respuesta aceptada

Richard Zapor
Richard Zapor el 1 de Jun. de 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 comentarios
Walter Roberson
Walter Roberson el 1 de Jun. de 2012
Small adjustment: underscore should also be in the [a-zA-Z0-9] part.
Felix Mardt
Felix Mardt el 20 de Nov. de 2020
Another small adjustment: single letter variables will not be returned. I am using
[a-zA-Z]([a-zA-Z0-9_]+)?
to cover them

Iniciar sesión para comentar.

Más respuestas (2)

per isakson
per isakson el 1 de Jun. de 2012
See FEX contribution: mparser
  1 comentario
Walter Roberson
Walter Roberson el 1 de Jun. de 2012
Interesting! I had never encountered that one!

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 20 de Mayo de 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.)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by