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

4 views (last 30 days)
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.

Answers (2)

madhan ravi
madhan ravi on 13 Jan 2019
Edited: madhan ravi on 13 Jan 2019
help diary
doc diary

Walter Roberson
Walter Roberson on 13 Jan 2019
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.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!