Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Export COMSOL FEM mesh to MATLAB

Subject: Export COMSOL FEM mesh to MATLAB

From: Vivek Saxena

Date: 16 Apr, 2010 10:59:05

Message: 1 of 5

PS -- Previous threads with similar subjects did not address my question, hence I'm asking a new one.

Hi,

I want to extract the following information from a COMSOL mesh

1. x and y coordinates of nodes
2. element connectivity array
3. list of boundary edges (inner and outer boundary)

and store them in the MATLAB workspace for use in my own code.

How can this be done?

Thanks in advance!

Subject: Export COMSOL FEM mesh to MATLAB

From: ilias Papalaios

Date: 9 Aug, 2010 10:58:05

Message: 2 of 5

"Vivek Saxena" <maverick280857@yahoo.com> wrote in message <hq9ftp$k0r$1@fred.mathworks.com>...
> PS -- Previous threads with similar subjects did not address my question, hence I'm asking a new one.
>
> Hi,
>
> I want to extract the following information from a COMSOL mesh
>
> 1. x and y coordinates of nodes
> 2. element connectivity array
> 3. list of boundary edges (inner and outer boundary)
>
> and store them in the MATLAB workspace for use in my own code.
>
> How can this be done?
>
> Thanks in advance!

These are crucial questions to me, too i am aware for responces

Subject: Export COMSOL FEM mesh to MATLAB

From: us

Date: 9 Aug, 2010 11:07:05

Message: 3 of 5

"ilias Papalaios" <papalaios@gmail.com> wrote in message <i3omvt$spv$1@fred.mathworks.com>...
> "Vivek Saxena" <maverick280857@yahoo.com> wrote in message <hq9ftp$k0r$1@fred.mathworks.com>...
> > PS -- Previous threads with similar subjects did not address my question, hence I'm asking a new one.
> >
> > Hi,
> >
> > I want to extract the following information from a COMSOL mesh
> >
> > 1. x and y coordinates of nodes
> > 2. element connectivity array
> > 3. list of boundary edges (inner and outer boundary)
> >
> > and store them in the MATLAB workspace for use in my own code.
> >
> > How can this be done?
> >
> > Thanks in advance!
>
> These are crucial questions to me, too i am aware for responces

a hint:
- ask the company...

http://www.comsol.com/support/contact/

us

Subject: Export COMSOL FEM mesh to MATLAB

From: Guillaume

Date: 14 Feb, 2013 13:49:08

Message: 4 of 5

"Vivek Saxena" wrote in message <hq9ftp$k0r$1@fred.mathworks.com>...
> PS -- Previous threads with similar subjects did not address my question, hence I'm asking a new one.
>
> Hi,
>
> I want to extract the following information from a COMSOL mesh
>
> 1. x and y coordinates of nodes
> 2. element connectivity array
> 3. list of boundary edges (inner and outer boundary)
>
> and store them in the MATLAB workspace for use in my own code.
>
> How can this be done?
>
> Thanks in advance!

Hi,

you can run a simple matlab script which reads the datas from the *.mphtxt file.

First, export the mesh from comsol as a *mphtxt file in the matlab working folder

then, you can run the following function (I've tested for a 2D triangular mesh). The function returns the coordinates of the nodes in a first matrix, and the connectivity array in the second one.

function [ meshPointMatrix, elementMatrix ] = plotComsolMesh( meshName )
i = 1;
identifier = {'# Mesh point coordinates', '# Elements'};
fid = fopen(strcat(meshName, '.mphtxt'));
%type (strcat(meshName, '.mphtxt'));
while ~feof(fid)
    readLine = cellstr(fgets(fid));
    if strcmp(readLine, identifier(1))
        while(~strcmp(readLine, cellstr('')))
            readLine = cellstr(fgets(fid));
            meshPoint(i, :) = readLine;
            i = i+1;
        end
        meshPoint(end, :) = [];
        for i = 1:size(meshPoint, 1)
            meshPointMatrix(i, :) = str2num(cell2mat(meshPoint(i)));
        end
        clear 'meshPoint';
        i = 1;
        
    end
    if strcmp(readLine, identifier(2))
        while(~strcmp(readLine, cellstr('')))
            readLine = cellstr(fgets(fid));
            element(i, :) = readLine;
            i = i+1;
        end
        element(end, :) = [];
        elementMatrix = str2num(cell2mat(element(1)));
        for i = 1:size(element, 1)
            x = str2num(cell2mat(element(i)));
            elementMatrix = [elementMatrix; x];
        end
        %clear 'element';
        i = 1;
    end
end
fclose(fid);
figure(1), clf, hold on, axis equal,
for i = 1:size(elementMatrix, 1)
    plot([meshPointMatrix(elementMatrix(i, 1)+1,1),meshPointMatrix(elementMatrix(i, 2)+1,1)],[meshPointMatrix(elementMatrix(i, 1)+1,2),meshPointMatrix(elementMatrix(i, 2)+1,2)], '-k');
    plot([meshPointMatrix(elementMatrix(i, 2)+1,1),meshPointMatrix(elementMatrix(i, 3)+1,1)],[meshPointMatrix(elementMatrix(i, 2)+1,2),meshPointMatrix(elementMatrix(i, 3)+1,2)], '-k');
    plot([meshPointMatrix(elementMatrix(i, 3)+1,1),meshPointMatrix(elementMatrix(i, 1)+1,1)],[meshPointMatrix(elementMatrix(i, 3)+1,2),meshPointMatrix(elementMatrix(i, 1)+1,2)], '-k');
end
end

Subject: Export COMSOL FEM mesh to MATLAB

From: Nicolas

Date: 23 Apr, 2014 17:28:12

Message: 5 of 5

Hello,

I was wondering if this problem was solved.
exporting Comsol mesh (x,y,data) and trying to plot it back in matlab, because I want to exaggerate the mesh in the y direction. I cannot do that in COMSOL.

Thanks

"Vivek Saxena" wrote in message <hq9ftp$k0r$1@fred.mathworks.com>...
> PS -- Previous threads with similar subjects did not address my question, hence I'm asking a new one.
>
> Hi,
>
> I want to extract the following information from a COMSOL mesh
>
> 1. x and y coordinates of nodes
> 2. element connectivity array
> 3. list of boundary edges (inner and outer boundary)
>
> and store them in the MATLAB workspace for use in my own code.
>
> How can this be done?
>
> Thanks in advance!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us