Extract values from ArrayDouble in Matlab
- Brian Umberger
- Posts: 48
- Joined: Tue Aug 28, 2007 2:03 pm
Extract values from ArrayDouble in Matlab
In an effort to become more familiar with scripting in Matlab using the OpenSim API, I am trying to recreate the optimization problem from the C++ API Examples where the forward velocity of the forearm/hand segment center of mass is maximized.
After running a simulation with a trial set of control values, I am able to obtain the X-Y-Z components of the velocity of the forearm/hand segment using some code that was provided in the “common scripting commands” section of the documentation:
modelStatePair = OpenSimContext(s, osimModel);
-
-
-
simbodyEngine.getVelocity(s, osimModel.getBodySet().get('r_ulna_radius_hand'), massCenter, velocity);
velocityArray = ArrayDouble.getValuesFromVec3(velocity)
which returns:
velocityArray =
2.92982 -1.70856 0.174449
The first number 2.92982 (negated) is the objective function value in this optimization problem. My question is, how do I extract/read/convert these velocity values into a Matlab numeric array? All three numbers are in a 1x1 variable of class org.opensim.modeling.ArrayDouble and I haven’t been able to figure out how to access them as individual numeric elements. I’ve looked at several other example scripts hoping I could find something similar or otherwise figure it out, but to no avail. I’m sure it is probably something simple, but it has me stumped.
Thanks for any assistance you can provide!
Brian
After running a simulation with a trial set of control values, I am able to obtain the X-Y-Z components of the velocity of the forearm/hand segment using some code that was provided in the “common scripting commands” section of the documentation:
modelStatePair = OpenSimContext(s, osimModel);
-
-
-
simbodyEngine.getVelocity(s, osimModel.getBodySet().get('r_ulna_radius_hand'), massCenter, velocity);
velocityArray = ArrayDouble.getValuesFromVec3(velocity)
which returns:
velocityArray =
2.92982 -1.70856 0.174449
The first number 2.92982 (negated) is the objective function value in this optimization problem. My question is, how do I extract/read/convert these velocity values into a Matlab numeric array? All three numbers are in a 1x1 variable of class org.opensim.modeling.ArrayDouble and I haven’t been able to figure out how to access them as individual numeric elements. I’ve looked at several other example scripts hoping I could find something similar or otherwise figure it out, but to no avail. I’m sure it is probably something simple, but it has me stumped.
Thanks for any assistance you can provide!
Brian
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Extract values from ArrayDouble in Matlab
Hi Brian,
The class ArrayDouble has methods to getitem(index), append(value), setitem(index,value)
If you use 'tab' in Matlab you should get auto-completion of function names (available functions).
The code should get much simpler in the next version and going forward since SimTK core classes will be available for scripting as well.
Please let us know how that goes or if you have any questions.
Best regards,
-Ayman
The class ArrayDouble has methods to getitem(index), append(value), setitem(index,value)
If you use 'tab' in Matlab you should get auto-completion of function names (available functions).
The code should get much simpler in the next version and going forward since SimTK core classes will be available for scripting as well.
Please let us know how that goes or if you have any questions.
Best regards,
-Ayman
- Brian Umberger
- Posts: 48
- Joined: Tue Aug 28, 2007 2:03 pm
Re: Extract values from ArrayDouble in Matlab
Hi Ayman,
Thanks for the speedy and helpful reply, getitem(index) worked great in this case. Thanks also for the suggestion to use tab auto completion to get a list of available functions. I did find though, that some functions worked while some did not. As an example, hitting tab after:
var = osimModel.get
suggests getMuscles and getControls as available functions (along with dozens of others). getMuscles works, but getControls results in:
var = osimModel.getControls
No method 'getControls' with matching signature found for class
'org.opensim.modeling.Model'.
I just used these two examples as they were both things I was trying to do in my current code. Most of what I've tried has worked, but I've come across several functions that return similar error messages. If I'm doing something wrong with getControls, please let me know; however, what I'm working on right now is not time sensitive, so if this is an issue that will be addressed or will become easier to work with in the next release as you suggested, then I can certainly wait.
Thanks again for the assistance!
Brian
Thanks for the speedy and helpful reply, getitem(index) worked great in this case. Thanks also for the suggestion to use tab auto completion to get a list of available functions. I did find though, that some functions worked while some did not. As an example, hitting tab after:
var = osimModel.get
suggests getMuscles and getControls as available functions (along with dozens of others). getMuscles works, but getControls results in:
var = osimModel.getControls
No method 'getControls' with matching signature found for class
'org.opensim.modeling.Model'.
I just used these two examples as they were both things I was trying to do in my current code. Most of what I've tried has worked, but I've come across several functions that return similar error messages. If I'm doing something wrong with getControls, please let me know; however, what I'm working on right now is not time sensitive, so if this is an issue that will be addressed or will become easier to work with in the next release as you suggested, then I can certainly wait.
Thanks again for the assistance!
Brian
- Jiang Ping
- Posts: 132
- Joined: Sun Aug 26, 2012 4:09 am
Re: Extract values from ArrayDouble in Matlab
osimModel.getControls(org.opensim.modeling.SWIGTYPE_p_SimTK_State)brian wrote:Hi Ayman,
Thanks for the speedy and helpful reply, getitem(index) worked great in this case. Thanks also for the suggestion to use tab auto completion to get a list of available functions. I did find though, that some functions worked while some did not. As an example, hitting tab after:
var = osimModel.get
suggests getMuscles and getControls as available functions (along with dozens of others). getMuscles works, but getControls results in:
var = osimModel.getControls
No method 'getControls' with matching signature found for class
'org.opensim.modeling.Model'.
I just used these two examples as they were both things I was trying to do in my current code. Most of what I've tried has worked, but I've come across several functions that return similar error messages. If I'm doing something wrong with getControls, please let me know; however, what I'm working on right now is not time sensitive, so if this is an issue that will be addressed or will become easier to work with in the next release as you suggested, then I can certainly wait.
Thanks again for the assistance!
Brian
so, it should be "osimModel.getControls(s)"
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Extract values from ArrayDouble in Matlab
Thanks Jiang.
Auto-completion systems offer different features, inside VisualStudio or other IDE's you usually see the full signature of the method including the arguments (and also the documentation in better IDEs). Matlab auto-completion only offers the method name. We generally recommend using Doxygen as the main source of code/API documentation
https://simtk.org/api_docs/opensim/api_docs30/
This should show the complete method names, including arguments and the documentation written by the developers.
Hope this helps,
-Ayman
Auto-completion systems offer different features, inside VisualStudio or other IDE's you usually see the full signature of the method including the arguments (and also the documentation in better IDEs). Matlab auto-completion only offers the method name. We generally recommend using Doxygen as the main source of code/API documentation
https://simtk.org/api_docs/opensim/api_docs30/
This should show the complete method names, including arguments and the documentation written by the developers.
Hope this helps,
-Ayman
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Extract values from ArrayDouble in Matlab
Hi Brian,
We just posted a beta version of 3.1 that should have the wrapping of the key simtk classes available. Please install it and let us know how it goes.
Best regards,
-Ayman
We just posted a beta version of 3.1 that should have the wrapping of the key simtk classes available. Please install it and let us know how it goes.
Best regards,
-Ayman
- Brian Umberger
- Posts: 48
- Joined: Tue Aug 28, 2007 2:03 pm
Re: Extract values from ArrayDouble in Matlab
First, thanks so much to Ayman and Jiang for the helpful replies. And Ayman, I will try out the 3.1 Beta and let you know how I make out.
I nearly have my exercise to recreate the C++ API optimization example working (i.e., to maximize the forward velocity of the arm). The only part I can't seem to get is updating the default control values with the new (trial) values when the Matlab function that computes the objective function is called. The corresponding line of code in the C++ example is:
osimModel.updDefaultControls() = newControls;
which makes sense when I look at the Doxygen documentation, but that does not work in Matlab, as Matlab doesn't like the lack of a subscript in what it thinks is an indexed expression on the left hand side. Is there a way to modify this line of code so that it will work in Matlab, or does updating the values of the controls simply need to be approached differently? Any suggestion are again most welcome!
Best,
Brian
I nearly have my exercise to recreate the C++ API optimization example working (i.e., to maximize the forward velocity of the arm). The only part I can't seem to get is updating the default control values with the new (trial) values when the Matlab function that computes the objective function is called. The corresponding line of code in the C++ example is:
osimModel.updDefaultControls() = newControls;
which makes sense when I look at the Doxygen documentation, but that does not work in Matlab, as Matlab doesn't like the lack of a subscript in what it thinks is an indexed expression on the left hand side. Is there a way to modify this line of code so that it will work in Matlab, or does updating the values of the controls simply need to be approached differently? Any suggestion are again most welcome!
Best,
Brian
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Extract values from ArrayDouble in Matlab
Hi Brian,
The latest beta version have the methods:
Model.setControls(State s, Vector controls)
which should do the trick.
Please let me know how that works.
Best regards,
-Ayman
The latest beta version have the methods:
Model.setControls(State s, Vector controls)
which should do the trick.
Please let me know how that works.
Best regards,
-Ayman
- Brian Umberger
- Posts: 48
- Joined: Tue Aug 28, 2007 2:03 pm
Re: Extract values from ArrayDouble in Matlab
Hi Ayman,
I appreciate your continued feedback and assistance. I'm running the 3.1 beta now and will let you know how I make out. I do have one question about Model.setControls. In some other "set" situations I have encountered, such as coordinate.setValue, a plain old double is passed in. However, it looks like I need to pass Model.setControls a SimTK Vector containing the new controls. Is there an "adaptor" I can use in Matlab to create this vector, similar to ArrayDouble.createVec3? If not, how do you create the necessary SimTK Vector in Matlab?
Thanks again!
Brian
I appreciate your continued feedback and assistance. I'm running the 3.1 beta now and will let you know how I make out. I do have one question about Model.setControls. In some other "set" situations I have encountered, such as coordinate.setValue, a plain old double is passed in. However, it looks like I need to pass Model.setControls a SimTK Vector containing the new controls. Is there an "adaptor" I can use in Matlab to create this vector, similar to ArrayDouble.createVec3? If not, how do you create the necessary SimTK Vector in Matlab?
Thanks again!
Brian
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Extract values from ArrayDouble in Matlab
Hi Brian,
In earlier versions we had to use convenience methods to make/use objects that are in the SimTK namespace. With version 3.1 these classes are available for instantiation/use so you can do:
myVector = org.opensim.modeling.Vector(size,initialValue)
r = myVector.get(index)
and
myVector.set(index, valueAtindex)
Hope this helps and please let me know how that goes or if you have any questions.
Best regards,
-Ayman
In earlier versions we had to use convenience methods to make/use objects that are in the SimTK namespace. With version 3.1 these classes are available for instantiation/use so you can do:
myVector = org.opensim.modeling.Vector(size,initialValue)
r = myVector.get(index)
and
myVector.set(index, valueAtindex)
Hope this helps and please let me know how that goes or if you have any questions.
Best regards,
-Ayman