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.

Exporting Comsol data to Matlab?

Please login with a confirmed email address before reporting spam

Hello everybody,

I want to postprocess Comsol data in Matlab. Since Ascii files blow up in size and I get "out of memory error", I'm looking for some other way. I've no experience with Comsol-Matlab interface and it seems to me that this interface is not working in my system.

Could someone please tell me, how to export Postprocessing data in some Matlab-compatible format (not Ascii)?

I've converted a Comsol model (after executing) into an m-file and wanted to run it in Matlab (running separately from Comsol) but Matlab is probably not able to read the flbinaryfile (*.mphm). Any suggestion, what could be wrong here?

Thanks.
Daniel

14 Replies Last Post 14 févr. 2017, 13:12 UTC−5
Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 18 nov. 2009, 17:06 UTC−5
Hi

You need to get your link between COMSOL and Matlab up running, either you missed a step during the installation of COMSOL or some of the files added to matlab are not set up correctly. Have a look at the installation documentation of COMSOL (I beleive ther are other comments in the forum and on the support KB pages).

Once you have the link running, you can (in V3.5a) just do a File "connect" to matlab from the COSOL GUI and export the "fem" structure to Matlab by a CNTRL-F.

then you need to understand the "fem" structure (see mlinterface.pdf in the COMSOL doc) to extract the data in binay mode and you can continue your processing at will.

Good luck
Ivar
Hi You need to get your link between COMSOL and Matlab up running, either you missed a step during the installation of COMSOL or some of the files added to matlab are not set up correctly. Have a look at the installation documentation of COMSOL (I beleive ther are other comments in the forum and on the support KB pages). Once you have the link running, you can (in V3.5a) just do a File "connect" to matlab from the COSOL GUI and export the "fem" structure to Matlab by a CNTRL-F. then you need to understand the "fem" structure (see mlinterface.pdf in the COMSOL doc) to extract the data in binay mode and you can continue your processing at will. Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 9 juin 2010, 05:12 UTC−4
Hi Ivar,

I have seen your reply for exporting data from comsol to matlab but when I do as u said then I get the following error
Error: 9016
Could not obtain license for SCRIPTPROMPT
License error.
Hi Ivar, I have seen your reply for exporting data from comsol to matlab but when I do as u said then I get the following error Error: 9016 Could not obtain license for SCRIPTPROMPT License error.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 30 mai 2011, 09:02 UTC−4
Hello everybody,

I want to export COMSOL data (maximum von mises stress) in to Matlab. Then I want to compare the value of the maximum von mises stress from the model with an in advanced determined permissible value, to create an optimal geometry for contact lenses. I've less experience with COMSOL and MATLAB interface.

Can someone tell me how to export the specific data to Matlab? In case it's necessary to use my model, I've attached my model.

Thanks in advance!

Arnoud
Hello everybody, I want to export COMSOL data (maximum von mises stress) in to Matlab. Then I want to compare the value of the maximum von mises stress from the model with an in advanced determined permissible value, to create an optimal geometry for contact lenses. I've less experience with COMSOL and MATLAB interface. Can someone tell me how to export the specific data to Matlab? In case it's necessary to use my model, I've attached my model. Thanks in advance! Arnoud


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 3 juin 2011, 09:15 UTC−4
Hi,

Did you find out the way to export the vonMises stress per element basis into MATLAB?

My requirement is to do it in V4.1, but not in V3.5a. I know that there are different access methods to interact with the data models in V4.1. For example, I figured out how to get/set domain information on geomety objects. However, the solid mechanis variables appear to be not documented yet.

Best regards,
Kodanda
Hi, Did you find out the way to export the vonMises stress per element basis into MATLAB? My requirement is to do it in V4.1, but not in V3.5a. I know that there are different access methods to interact with the data models in V4.1. For example, I figured out how to get/set domain information on geomety objects. However, the solid mechanis variables appear to be not documented yet. Best regards, Kodanda

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 4 juin 2011, 13:27 UTC−4
Hi,

I didn't discover the way to export the maximum von Mises stress from COMSOL to MATLAB. I've read the document llmatlab_ug.pdf, but it's hard to understand for a MATLAB and COMSOL rookie like me. I'm using COMSOL 4.0a by the way. Do you have any experience with exporting data from COMSOL to MATLAB in COMSOL 4.0a or is it maybe the same as 4.1?

Best regards,
Arnoud
Hi, I didn't discover the way to export the maximum von Mises stress from COMSOL to MATLAB. I've read the document llmatlab_ug.pdf, but it's hard to understand for a MATLAB and COMSOL rookie like me. I'm using COMSOL 4.0a by the way. Do you have any experience with exporting data from COMSOL to MATLAB in COMSOL 4.0a or is it maybe the same as 4.1? Best regards, Arnoud

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 17 juin 2011, 09:17 UTC−4

Hi

You need to get your link between COMSOL and Matlab up running, either you missed a step during the installation of COMSOL or some of the files added to matlab are not set up correctly. Have a look at the installation documentation of COMSOL (I beleive ther are other comments in the forum and on the support KB pages).

Once you have the link running, you can (in V3.5a) just do a File "connect" to matlab from the COSOL GUI and export the "fem" structure to Matlab by a CNTRL-F.

then you need to understand the "fem" structure (see mlinterface.pdf in the COMSOL doc) to extract the data in binay mode and you can continue your processing at will.

Good luck
Ivar



Sorry, but this answer does not help.

First of all "when or where" do I have to press ctrl+f. Second I of course want to automatically return values that comsol computes to the matlab script (i.e. without gui ... Matlab calls Comsol and then uses the matrix that comsol produces for further Processing).

Second ... mlinterface.doc - I have not found it and cannot find it. Is it to much to ask for a link if there is a documentation...

The comsol Matlab interface is really a joke. It is a real struggle to do the simplest thing like give a single value from Comsol to Matlab. Sorry, but this is unacceptable.

I hope you will prepare a good solution soon, as I see the necessity to spread this word in the community...

Michael
[QUOTE] Hi You need to get your link between COMSOL and Matlab up running, either you missed a step during the installation of COMSOL or some of the files added to matlab are not set up correctly. Have a look at the installation documentation of COMSOL (I beleive ther are other comments in the forum and on the support KB pages). Once you have the link running, you can (in V3.5a) just do a File "connect" to matlab from the COSOL GUI and export the "fem" structure to Matlab by a CNTRL-F. then you need to understand the "fem" structure (see mlinterface.pdf in the COMSOL doc) to extract the data in binay mode and you can continue your processing at will. Good luck Ivar [/QUOTE] Sorry, but this answer does not help. First of all "when or where" do I have to press ctrl+f. Second I of course want to automatically return values that comsol computes to the matlab script (i.e. without gui ... Matlab calls Comsol and then uses the matrix that comsol produces for further Processing). Second ... mlinterface.doc - I have not found it and cannot find it. Is it to much to ask for a link if there is a documentation... The comsol Matlab interface is really a joke. It is a real struggle to do the simplest thing like give a single value from Comsol to Matlab. Sorry, but this is unacceptable. I hope you will prepare a good solution soon, as I see the necessity to spread this word in the community... Michael

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 4 juil. 2011, 02:20 UTC−4

Hi,

I didn't discover the way to export the maximum von Mises stress from COMSOL to MATLAB. I've read the document llmatlab_ug.pdf, but it's hard to understand for a MATLAB and COMSOL rookie like me. I'm using COMSOL 4.0a by the way. Do you have any experience with exporting data from COMSOL to MATLAB in COMSOL 4.0a or is it maybe the same as 4.1?

Best regards,
Arnoud


Hello,

it need this information too. :)
I want to export only the von Mises stress data calculated in Comsol into Matlab for postprocessing. I know how to export to an txt-file. Of course I can copy paste the whole data, but this is to much handwork.

Greetings
[QUOTE] Hi, I didn't discover the way to export the maximum von Mises stress from COMSOL to MATLAB. I've read the document llmatlab_ug.pdf, but it's hard to understand for a MATLAB and COMSOL rookie like me. I'm using COMSOL 4.0a by the way. Do you have any experience with exporting data from COMSOL to MATLAB in COMSOL 4.0a or is it maybe the same as 4.1? Best regards, Arnoud [/QUOTE] Hello, it need this information too. :) I want to export only the von Mises stress data calculated in Comsol into Matlab for postprocessing. I know how to export to an txt-file. Of course I can copy paste the whole data, but this is to much handwork. Greetings

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 8 févr. 2012, 17:42 UTC−5
Hello,
I know this must be a simple operation but how do you export just the eigenvalue/eigenfrequency in the new versions of Comsol? In 3.5a I used "x=fem.sol.lambda;"but now in Comsol 4.2 I have tried different variations of mpheval and mphglobal but have not been able to get the syntax right. Thanks for any assistance.
Hello, I know this must be a simple operation but how do you export just the eigenvalue/eigenfrequency in the new versions of Comsol? In 3.5a I used "x=fem.sol.lambda;"but now in Comsol 4.2 I have tried different variations of mpheval and mphglobal but have not been able to get the syntax right. Thanks for any assistance.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 10 févr. 2012, 07:30 UTC−5

Hello,
I know this must be a simple operation but how do you export just the eigenvalue/eigenfrequency in the new versions of Comsol? In 3.5a I used "x=fem.sol.lambda;"but now in Comsol 4.2 I have tried different variations of mpheval and mphglobal but have not been able to get the syntax right. Thanks for any assistance.


One way is to reverse engineer things that are not yet documented. For your purpose, there is an "Export Data" node available for the Results section. Right click on the results node and add an Export. There you will be able to select the different native and user-derived variable (if any) as export data. Save the model as an .m file and open it in a text editor, you will see the commands you can use in MATLAB.

best,
Kodanda
[QUOTE] Hello, I know this must be a simple operation but how do you export just the eigenvalue/eigenfrequency in the new versions of Comsol? In 3.5a I used "x=fem.sol.lambda;"but now in Comsol 4.2 I have tried different variations of mpheval and mphglobal but have not been able to get the syntax right. Thanks for any assistance. [/QUOTE] One way is to reverse engineer things that are not yet documented. For your purpose, there is an "Export Data" node available for the Results section. Right click on the results node and add an Export. There you will be able to select the different native and user-derived variable (if any) as export data. Save the model as an .m file and open it in a text editor, you will see the commands you can use in MATLAB. best, Kodanda

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 19 août 2012, 20:54 UTC−4
Hello Kodanda Ram Mangipudi,

I did so as you said.
There are some new lines added to my .m file as follow:

model.result.export.create('data1', 'Data');
model.result.export('data1').set('expr', {'emw.Ex'});
model.result.export('data1').set('descr', {'Electric field, x component'});
model.result('pg1').run;
model.result.export('data1').set('expr', {'emw.Ex' 'lambda'});
model.result.export('data1').set('descr', {'Electric field, x component' 'Eigenvalue'});

But still I dont know what command I should use to get my Eigenvalue or to plot emw.Ex.

Thanks
Hello Kodanda Ram Mangipudi, I did so as you said. There are some new lines added to my .m file as follow: model.result.export.create('data1', 'Data'); model.result.export('data1').set('expr', {'emw.Ex'}); model.result.export('data1').set('descr', {'Electric field, x component'}); model.result('pg1').run; model.result.export('data1').set('expr', {'emw.Ex' 'lambda'}); model.result.export('data1').set('descr', {'Electric field, x component' 'Eigenvalue'}); But still I dont know what command I should use to get my Eigenvalue or to plot emw.Ex. Thanks

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 27 nov. 2012, 12:21 UTC−5

Hello,
I know this must be a simple operation but how do you export just the eigenvalue/eigenfrequency in the new versions of Comsol? In 3.5a I used "x=fem.sol.lambda;"but now in Comsol 4.2 I have tried different variations of mpheval and mphglobal but have not been able to get the syntax right. Thanks for any assistance.


Hi,

Has a solution for this simple task been found? I am using version 4.3 in comsol.

Thanks,

Hamsa
[QUOTE] Hello, I know this must be a simple operation but how do you export just the eigenvalue/eigenfrequency in the new versions of Comsol? In 3.5a I used "x=fem.sol.lambda;"but now in Comsol 4.2 I have tried different variations of mpheval and mphglobal but have not been able to get the syntax right. Thanks for any assistance. [/QUOTE] Hi, Has a solution for this simple task been found? I am using version 4.3 in comsol. Thanks, Hamsa

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 nov. 2012, 03:25 UTC−5
Hi,

The solution is posted above by Iman Aryanfar .

Repeat what Iman Aryanfar has did and showed the code. I suggest you to export only one variable while learning the command syntaxes, so you wont get confused.

Best of luck,
Kodanda
Hi, The solution is posted above by Iman Aryanfar . Repeat what Iman Aryanfar has did and showed the code. I suggest you to export only one variable while learning the command syntaxes, so you wont get confused. Best of luck, Kodanda

Please login with a confirmed email address before reporting spam

Posted: 7 years ago 14 févr. 2017, 11:35 UTC−5
As this is a pain to find, I'll post a bit more complete answer here (5 years later...):


model.result.export('data1').set('unit', {'' '' ''});
model.result.export('data1').set('descr', {'Velocity field, x component' 'Velocity field, y component' 'Pressure'});
model.result.export('data1').set('expr', {'u' 'v' 'p'});
model.result.export('data1').set('filename', 'u_v_p.csv');
model.result.export('data1').run;


The last two lines are the most important ones. You have to set the output _and_ run. The above code stores the solution u,v,p to filename 'u_v_p.csv'. The logic is that first you set a bunch of parameters and then you run the export.

After this read the csv file using matlab commands.
As this is a pain to find, I'll post a bit more complete answer here (5 years later...): model.result.export('data1').set('unit', {'' '' ''}); model.result.export('data1').set('descr', {'Velocity field, x component' 'Velocity field, y component' 'Pressure'}); model.result.export('data1').set('expr', {'u' 'v' 'p'}); model.result.export('data1').set('filename', 'u_v_p.csv'); model.result.export('data1').run; The last two lines are the most important ones. You have to set the output _and_ run. The above code stores the solution u,v,p to filename 'u_v_p.csv'. The logic is that first you set a bunch of parameters and then you run the export. After this read the csv file using matlab commands.

Walter Frei COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 7 years ago 14 févr. 2017, 13:12 UTC−5
Hello,
With regards to exporting data from COMSOL to MATLAB, we do of course first recommend that you use the LiveLink for MATLAB product for this. Since some of the earlier comments in this thread the documentation has also been expanded with many more examples and code snippets that you will find useful, so please make sure to go through the Introduction to LiveLink for MATLAB documentation and to skim over the LiveLink for MATLAB User Guide, as a lot of the questions here are directly answered therein.

Now, if you have very limited needs and want to and avoid using the LiveLink, you can write out data from the model directly to a text file. Of course there are a number of different formats into which you might like to write the data. This is where the Application Builder will become quite useful. For an example, please see:
www.comsol.com/blogs/exporting-meshes-and-solutions-using-the-application-builder/
Note that you can use the Application Builder within the core functionality of COMSOL Multiphysics, as long as you are running on the Windows platform.
Best Regards,
Hello, With regards to exporting data from COMSOL to MATLAB, we do of course first recommend that you use the LiveLink for MATLAB product for this. Since some of the earlier comments in this thread the documentation has also been expanded with many more examples and code snippets that you will find useful, so please make sure to go through the Introduction to LiveLink for MATLAB documentation and to skim over the LiveLink for MATLAB User Guide, as a lot of the questions here are directly answered therein. Now, if you have very limited needs and want to and avoid using the LiveLink, you can write out data from the model directly to a text file. Of course there are a number of different formats into which you might like to write the data. This is where the Application Builder will become quite useful. For an example, please see: www.comsol.com/blogs/exporting-meshes-and-solutions-using-the-application-builder/ Note that you can use the Application Builder within the core functionality of COMSOL Multiphysics, as long as you are running on the Windows platform. Best Regards,

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.