# Can I use a symbol as a variable?

조회 수: 2 (최근 30일)
Prasenjit Dewanjee 2023년 2월 19일
댓글: Walter Roberson 2023년 2월 19일
HI!
In my code, I have declared:
syms m
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:
m= 1:1:20
Is there any way to do this?
Any help will be appreciated.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Paul 2023년 2월 19일
Hi Prasenjit,
Yes, you can do that
syms m
f = m^2 % expression that contains m as a symbol
f =
whos m % m is sym
Name Size Bytes Class Attributes m 1x1 8 sym
for m = 1:1:20; % loop with m
m; % do something with m
end
whos m % m is now a double
Name Size Bytes Class Attributes m 1x1 8 double
At this point f is still an expression in terms of m
f
f =
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.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Walter Roberson 2023년 2월 19일
if the idea is that you want the loop variable m to become the value of symbolic m, then see subs()

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Code Performance에 대해 자세히 알아보기

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by