How to transform matlab output to a 'matlab code' form?

조회 수: 10 (최근 30일)
Weijia Yao
Weijia Yao 2019년 1월 13일
답변: Walter Roberson 2019년 1월 13일
I am using symbolic expressions and symbolic functions to do some complicated calculations, such as computing a Jacobian of a vector fuction. Then the output is something like this
[a,b]
[c,d]
where a,b,c,d are symbols. This output is a matrix with a,b,c,d as entries, and it is very intuitive. However, I want the output to become [a,b; c,d], something that I can direcly 'copy-paste' to my matlab code and interpret by matlab directly. Of course I can manually change the output matrix to the 'matlab code' form, but sometimes my output is very complicated and I do not want to do it manually every time.

답변 (2개)

madhan ravi
madhan ravi 2019년 1월 13일
편집: madhan ravi 2019년 1월 13일
help diary
doc diary
  댓글 수: 1
Weijia Yao
Weijia Yao 2019년 1월 13일
I think this command cannot save the output in the 'matlab code' style.

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


Walter Roberson
Walter Roberson 2019년 1월 13일
Use matlabFunction() instead.
copy/paste of symbolic expressions is like eval() on those expressions, which is a problem because symbolic expressions are in a language which is not exactly the same as MATLAB.
matlabFunction() generates anonymous functions that can then be executed on inputs to produce numeric results.
I recommend using the 'vars' option of matlabFunction to be sure that your variables are in the same order.
If you have inputs that should be vectors, then you should definitely use the 'vars' option. Look carefully at the {} syntax:
'vars', {[x0 x1 x2], a, b}
would created three inputs, the first of which expects arrays with three columns in which the corresponding rows together forming [x0, x1, x2] entries. Using [x0;x1;x2] there instead would create an input in which arrays with 3 rows were expected with the columns giving the x0, x1, x2 entries.

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by