Can I use a symbol as a variable?
1 view (last 30 days)
Show older comments
Prasenjit Dewanjee on 19 Feb 2023
Commented: Walter Roberson on 19 Feb 2023
In my code, I have declared:
I have thes huge expressions that contain m in them as a symbol. Later in the code, I want to use m as a varibale in a loop like this:
Is there any way to do this?
Any help will be appreciated.
Paul on 19 Feb 2023
Yes, you can do that
f = m^2 % expression that contains m as a symbol
whos m % m is sym
for m = 1:1:20; % loop with m
m; % do something with m
whos m % m is now a double
At this point f is still an expression in terms of m
However, the m that is in f is now not related to the variable m in the Matlab workspace. Are you sure that's what you want?
What is the goal of defining expressions in terms of sym m and then using m as a loop variable? If you show a simple example of what you're trying to do someone might be able to help do it in a different or better way.
Walter Roberson on 19 Feb 2023
if the idea is that you want the loop variable m to become the value of symbolic m, then see subs()
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!