How do I find a variable number of functions?

3 views (last 30 days)
Hello everyone How do I find a variable number of functions? For example, f = x1 + 2 * x2 two variables and variables [x1 x2] 2 variables f = 2 * x + y ^ 2 + u ^ 3 three variables and variables [x y u] 3 variables Thank you very much, everybody.
  2 Comments
the cyclist
the cyclist on 15 Oct 2014
I am not sure what you mean by "find" here. What is your input? What do you expect your output to be. State it in terms of MATLAB, not just a description. We only know what you tell us. Contrary to popular belief, we cannot read minds.
Volkan
Volkan on 15 Oct 2014
Edited: the cyclist on 15 Oct 2014
I'm looking for a command as I mentioned in the following example.
a=????(x1 + 2 * x2)
b=?????(x1 + 2 * x2)
a=2
b=[x1 x2]
or
c=????(2 * x + y ^ 2 + u ^ 3)
d=?????(2 * x + y ^ 2 + u ^ 3)
c=3
d=[x y u]

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Oct 2014
Use the symvar function, then the length or size functions.
Using symvar:
f1 = 'x1 + 2 * x2'
f2 = '2 * x + y ^ 2 + u ^ 3'
s1 = symvar(f1)
s2 = symvar(f2)
produces:
s1 =
'x1'
'x2'
s2 =
'u'
'x'
'y'
That is as good as it gets, though. You have to cast the expressions as strings, and they cannot be function handles. I cannot find any other functions that would do what you want.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!