Splitting a string into variables without knowing what variable names might be.

5 views (last 30 days)
I'm working on a matlab script which will take in syntax from a programming language (cinnamon roll, which was a specific user created script by someone else without much documentation) and will make new .m files to be run by a separate MATLAB file. Here's my question with what i'm given.
From Cinnamon Roll Syntax documentation
MATLAB_CMD
Performs a MATLAB command and passes the value to a single Output variable.
• Required argument #1: Output variable. Must be a parameter, freeze, or script variable. If no output,
use "[]" to indicate.
• Required argument #2: MATLAB command or script. Must be in quotes.
• Optional arguments: Input variable(s) or value(s). The arguments must be seperated by a space.
• Examples:
;Generate a value from the uniform distribution on the interval (a, b)
matlab_cmd myVar "a + (b-a)*rand(1);" 0 10
matlab_cmd [] "uiwait(msgbox('Pause until the user acknowledges','Wait','modal'));"
My question is as follows. If i'm given "matlab_cmd myVar "a + (b-a)*rand(1);" 0 10", I know I can tell MATLAB programatically to expect 2 inputs. I therefore would look for the ' " ' at the start and end of the command which will be passed to MATLAB and then there will be a large amount of string parsing. Therefore, i'm left with just:
"a + (b-a)*rand(1);"
This leads me to the following questions.
  1. How can I programatically parse out the variables "a" and "b" so that MATLAB can understand that it will need to write "a = 0" and "b= 10"?
  2. How can I make sure matlab ignores the function call "rand" since that generates a random number?
  3. Say if "a" was instead another variable like "alpha" and "b" was instead another variable like "beta", how can I make sure MATLAB understands these are still variable names? I'd prefer to not add in programming for the user to specify what the variable names are, but if that's what I have to do, then i'll do it.
I hope i've explained this well enough for someone to follow. If there are any questions, please post them down below and i'll try to answer them while I continue to work on this.
  2 Comments
Stephen23
Stephen23 on 3 Feb 2022
Edited: Stephen23 on 3 Feb 2022
A general solution for what you are trying to achieve essentially requires replicating the MATLAB parser.
Note for example that MATLAB functions and variable indexing both use parentheses, that function scope can change during runtime, and dynamic creation of variables. In practice these mean that static code analysis (as you are attempting) cannot guarantee the behavior of MATLAB code, or even if something will be a function or a variable when run. So what you are asking for has no general solution other than to parse and run the code.
"I'd prefer to not add in programming for the user to specify what the variable names are..."
This is probably much simpler and more robust approach. If the user specifies the variables explicitly, this removes all ambiguity and would likely be much more efficient.
Walter Roberson
Walter Roberson on 3 Feb 2022
In the restricted case that no indexing is possible, that NAME(VALUE) is always a call to NAME passing in VALUE, then you can use symvar()

Sign in to comment.

Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!