Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

[Solved] Evaluate and access Derived Values in Matlab Livelink

Please login with a confirmed email address before reporting spam

The below post is related to an archived discussion


Hello fellow COMSOLer,

If you are reading this, you are probably looking for a way to update and access some value from the Derived Values node using Matlab Livelink. After struggling to find a solution online, I figured it out by exporting my COMSOL model as a .m script and then observing how the Derived Value operations were documented. This may be useful, for example, when you run your model iteratively through Matlab Livelink and want to view the desired value after each iteration (which was my case). So here goes:

Let's suppose:

-you already have an Average operation 'av1' under Derived Values in your model.

-the results of this model are stored in the dataset 'dset7'.

-the evaluations of 'av1' are stored in the table 'tbl11'.

model.result.numerical('av1').set('data', 'dset7'); model.result.numerical('av1').set('table', 'tbl11'); model.result.numerical('av1').appendResult;

table_of_values = mphtable(model,'tbl11');

In the Matlab variable Workspace, you will find the structure "table_of_values". The values you are looking for will be inside it in the field "data".

Let's say you want to access the latest value which you just evaluated: value_of_interest = table_of_values.data(1,end)

Hope this helps. Don't know if there is a more elegant way of doing it, but far better than opening the .mph file everytime after running the model in Matlab to evaluate and check the Derived Values.


5 Replies Last Post 08.03.2021, 10:54 GMT-5
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 4 years ago 12.01.2021, 03:34 GMT-5

One comment: I you don't need the data in a table you can just do:

newdata = model.result.numerical('av1').getReal

For complex data you have to use getImag as well.

Tables are useful for many purposes, but they take time to fill and takes up memory.

-------------------
Lars Gregersen
Comsol Denmark
One comment: I you don't need the data in a table you can just do: newdata = model.result.numerical('av1').getReal For complex data you have to use getImag as well. Tables are useful for many purposes, but they take time to fill and takes up memory.

Please login with a confirmed email address before reporting spam

Posted: 4 years ago 19.01.2021, 06:08 GMT-5

Hi Lars, Thanks for the much simpler alternative - it works!

Best, Vikas

Hi Lars, Thanks for the much simpler alternative - it works! Best, Vikas

Please login with a confirmed email address before reporting spam

Posted: 4 years ago 07.03.2021, 12:03 GMT-5

One comment: I you don't need the data in a table you can just do:

newdata = model.result.numerical('av1').getReal

For complex data you have to use getImag as well.

Tables are useful for many purposes, but they take time to fill and takes up memory.

I'd like to know in general how I can figure out the appropriate command. Suppose I have a derived value under the result node, if I right-click it in COMSOL and choose 'copy as code to clipboard', and then 'get', then I can get 'model.result().numerical("av1")'. I still don't know how to get the '.getReal' part. The Matlab livelink documentation doesn't say these at all. It does have 'getRealRow(<idx>)' as an example for table, but then the whole process seems trial-and-error to me without clear documentation.

>One comment: >I you don't need the data in a table you can just do: > > newdata = model.result.numerical('av1').getReal > >For complex data you have to use getImag as well. > >Tables are useful for many purposes, but they take time to fill and takes up memory. I'd like to know in general how I can figure out the appropriate command. Suppose I have a derived value under the result node, if I right-click it in COMSOL and choose 'copy as code to clipboard', and then 'get', then I can get 'model.result().numerical("av1")'. I still don't know how to get the '.getReal' part. The Matlab livelink documentation doesn't say these at all. It does have 'getRealRow()' as an example for table, but then the whole process seems trial-and-error to me without clear documentation.

Please login with a confirmed email address before reporting spam

Posted: 4 years ago 07.03.2021, 12:16 GMT-5

Hi there, yes I agree with your comment. It was hard for me too to figure out the '.getReal' part of the command in Matlab Livelink to extract the newly evaluated quantity under the Derived Values node. The approach of "right-click->copy as code to clipboard" works well for many other types of nodes (e.g. physics boundary conditions and even plots), but somehow not so intuitive for the Derived Values node. If it wasn't for Lars' answer above, I would have probably never discovered this elegant way to extract the result. I must admit though that I didn't go through the user guide of the Matlab Livelink in much detail. But if what you say is true, then it seems like it would've been hard for me to find it there as well.

Hi there, yes I agree with your comment. It was hard for me too to figure out the '.getReal' part of the command in Matlab Livelink to extract the newly evaluated quantity under the Derived Values node. The approach of "right-click->copy as code to clipboard" works well for many other types of nodes (e.g. physics boundary conditions and even plots), but somehow not so intuitive for the Derived Values node. If it wasn't for Lars' answer above, I would have probably never discovered this elegant way to extract the result. I must admit though that I didn't go through the user guide of the Matlab Livelink in much detail. But if what you say is true, then it seems like it would've been hard for me to find it there as well.

Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 4 years ago 08.03.2021, 10:54 GMT-5

LiveLink for Matlab has functions that makes life easier for you so you don't have to learn the whole COMSOL API.

E.g. for getting averages you would simply use the mphmean function and that would take care of all the details of settings things up, doing error handling and returned the (possibly complex) results to you. There are a ton of functions for getting data out of Comsol models.

Try

help mli

and see.

I you want to deal with the low level details you have to read the Programming Reference Manual, which for Comsol 5.6 is 970 pages. No one likes to read that many pages, but sometimes it becomes necessary to look through a few sections in order to get what you want. LiveLink for Matlab comes with a tool called mphnavigator that can help you with information about properties and the API that each node in the model tree supports.

We are adding functions based on feedback from customers so if there is anything you miss today or things you find difficult you are always welcome to write to support (or this forum) and we'll see what we can do.

-------------------
Lars Gregersen
Comsol Denmark
LiveLink for Matlab has functions that makes life easier for you so you don't have to learn the whole COMSOL API. E.g. for getting averages you would simply use the mphmean function and that would take care of all the details of settings things up, doing error handling and returned the (possibly complex) results to you. There are a ton of functions for getting data out of Comsol models. Try help mli and see. I you want to deal with the low level details you have to read the Programming Reference Manual, which for Comsol 5.6 is 970 pages. No one likes to read that many pages, but sometimes it becomes necessary to look through a few sections in order to get what you want. LiveLink for Matlab comes with a tool called mphnavigator that can help you with information about properties and the API that each node in the model tree supports. We are adding functions based on feedback from customers so if there is anything you miss today or things you find difficult you are always welcome to write to support (or this forum) and we'll see what we can do.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.