Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Tutorial: Working between COMSOL 4.1 and MATLAB 2010b

Ok, here is what I figured out in order to be able to take a COMSOL model, modify it in MATLAB and then reopen it in COMSOL for further work:

1. Launch COMSOL normally;
2. Launch MATLAB by using "COMSOL 4.1 with MATLAB";

In COMSOL:
- Saving your current COMSOL model into the COMSOL server:
a) File > Export Model to Server...
(Default settings: Server = localhost, Port = 2036)
Model = <filename> (Ex.: shell_diffusion)

(At that point, your COMSOL model is in the COMSOL server/buffer and is named "shell_diffusion")

In MATLAB:
- Loading your COMSOL model that you sent in the COMSOL server:
>> model = ModelUtil.model('shell_diffusion');
- Saving the file as a MATLAB .m-file:
>> model.save(<filename>, 'm'); (Ex.: model.save('shell_diffusion2', 'm'); )
(The file would be saved by default in the current working folder)
- In order to save the file in a specific folder:
>> model.save('C:\COMSOL_Models\shell_diffusion2', 'm');

(You now have created a MATLAB .m-file from a COMSOL file which you can edit and modify)
(I know, you could have just use the File > Save as Model M-File... option in COMSOL but I don't know how you could resend it to COMSOL that way)

- To save your modified model back into COMSOL:
>> model.save('C:\COMSOL_Models\shell_diffusion3'); (No specific file extension will save it as a .mph COMSOL file)

(You now have your COMSOL file back which you can reopen in COMSOL)

4 Replies Last Post Mar 3, 2013, 3:33 AM EST
Posted: 7 years ago Mar 8, 2011, 5:07 PM EST
Hi

Thanks for the tutorial, always useful, will check it out tomorrow

--
Good luck
Ivar
Hi Thanks for the tutorial, always useful, will check it out tomorrow -- Good luck Ivar

Posted: 7 years ago Mar 8, 2011, 5:20 PM EST
I just forgot one thing:

- How to reload my modified M-file into the "model" in order to save it as a .mph...

Humm...inconvenient...

More to come later...
I just forgot one thing: - How to reload my modified M-file into the "model" in order to save it as a .mph... Humm...inconvenient... More to come later...

Posted: 5 years ago Mar 2, 2013, 8:48 PM EST
Hi Ivar,
I have a problem linking Comsol to Matlab.
I have Comsol 4.3a, Matlab R2011b, and the Comsol with Matlab program that came with Comsol.
I opened both programs. Then I created a model in Comsol. If I try to access the client server to transfer the model, it says:" Timeout on Connection attempt."

I then tried to save the model as .m file and then open it in the server prompt, it says: "unknown source." Even if I try to manually save the model as .mph file and then reopen in Matlab, it says:"unknown source."

I've done all the necessary link commands in the beginning and they were accepted.
Please advise.

Thanks,

Fred
Hi Ivar, I have a problem linking Comsol to Matlab. I have Comsol 4.3a, Matlab R2011b, and the Comsol with Matlab program that came with Comsol. I opened both programs. Then I created a model in Comsol. If I try to access the client server to transfer the model, it says:" Timeout on Connection attempt." I then tried to save the model as .m file and then open it in the server prompt, it says: "unknown source." Even if I try to manually save the model as .mph file and then reopen in Matlab, it says:"unknown source." I've done all the necessary link commands in the beginning and they were accepted. Please advise. Thanks, Fred

Posted: 5 years ago Mar 3, 2013, 3:33 AM EST
Hi

Sorry but I'm no expert on Matlab to COMSOL liking. For me it works OK, but I had to reinstall COMSOL last time I updated Matlab. On the other hand there is a specal command in the COMSOl preferences to register matlab, I think, this should be done once, in case of problems.

Check the Knowledge Base (KB) there are a few entries, specifically for Matlab and COMSOL

--
Good luck
Ivar
Hi Sorry but I'm no expert on Matlab to COMSOL liking. For me it works OK, but I had to reinstall COMSOL last time I updated Matlab. On the other hand there is a specal command in the COMSOl preferences to register matlab, I think, this should be done once, in case of problems. Check the Knowledge Base (KB) there are a few entries, specifically for Matlab and COMSOL -- Good luck Ivar

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.