필터 지우기
필터 지우기

How to get struct value solX.x1 with a symbolic variable x1?

조회 수: 2 (최근 30일)
xv cheng
xv cheng 2018년 7월 31일
댓글: Walter Roberson 2018년 7월 31일
Hello!I have a symbolic expression like
expr=3*x1-2*x3
I solve a equation using vpasolve and get a struct called solX. I can use solX.x1,solX.x2,...to get values.
I want Matlab to find the variables name in the expr and use subs to get the expr value.
variable_Name=symvar(expr)% get 1*2 sym [x1 x3]
I know solX.name(1) is not correct. How to get solX.x1 using variable_Name?
Many thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2018년 7월 31일
solX.(name{1})
However since your goal is symbolic substitution you can probably just do
subs(Expression, solX)
  댓글 수: 2
xv cheng
xv cheng 2018년 7월 31일
편집: xv cheng 2018년 7월 31일
Thank you!
solX.(name{1})
It means name should be a cell array ,the element is char,
name={'x1' 'x2' 'x3' ...}
But name=symvar(expr) get 1*2 sym array [x1 x3].So solX.(name{1}) doesn't work.
But at last subs(Expression, solX) It's very simple and useful!Thank you very much!
Walter Roberson
Walter Roberson 2018년 7월 31일
name = arrayfun(@char, symvar(YourExpression), 'uniform', 0);
after which you can use solX.(name{1})
But that assumes that all of the symbols in YourExpression appear in solX. If you do not choose to use the shortcut that I posted before of subs(YourExpression, solX) then better would be
subs(YourExpression, fieldnames(solX), struct2cell(solX))

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

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by