How to make Structs with Symbolic Variables in Matlab
Show older comments
I would like to make a struct (or cell array) with symbolic variables that I can access like an object using Matlab Symbolic Toolbox (2018b). I would like something like this:
syms car.speed car.weight bus.speed bus.weight
so that I can access the elements like so:
car.KE = 0.5*car.speed^2*car.weight
bus.KE = 0.5*bus.speed^2*bus.weight
This works for normal variables but doesn't seem to work for in the symbolic toolbox.
Answers (2)
Alex Mcaulley
on 10 May 2019
syms speed weight
car.speed = speed;
car.weight = weight;
car.KE = 0.5*car.speed^2*car.weight;
1 Comment
Christian Korte
on 10 May 2019
Edited: Christian Korte
on 10 May 2019
Walter Roberson
on 10 May 2019
You would like to have a symbolic variable whose name includes a period at the MuPad level, or at least which got translated to a period when whatever internal mupad representation for it was brought back to the MATLAB level.
There is no direct support for that and it is not possible to do completely cleanly.
What you can do is
bus.speed = evalin(symengine, '`bus.speed`')
After that you can use bus.speed in symbolic expressions where it will display as `bus.speed` including the backquotes.
You might have difficulty with symvar and subs and the like: I did not test those.
MuPad permits fairly arbitrary characters in the names of its variables but only when surrounded by backquotes.
MuPad itself uses . as concatenation of tokens so you did
evalin(symengine, 'bus.speed')
You would get back busspeed
Categories
Find more on Common Operations 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!