- fopen - https://www.mathworks.com/help/matlab/ref/fopen.html
- feof - https://www.mathworks.com/help/matlab/ref/feof.html
- strtrim - https://www.mathworks.com/help/matlab/ref/strtrim.html
- startswith - https://www.mathworks.com/help/matlab/ref/startswith.html
- contains - https://www.mathworks.com/help/matlab/ref/string.contains.html
- sscanf - https://www.mathworks.com/help/matlab/ref/sscanf.html
How to read the .inp file generated in gmsh for extracting the Nodal coordinates and element connectivity of the mesh.
10 views (last 30 days)
Show older comments
I have a 2D structured mesh of a 2D domain, as shown, generated by gmsh, an open source meshing software. After generating the mesh, I exported the file as .inp file, which I have attached.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/698183/image.png)
From the line no. 373 to 596, are the element nodal connectivity. which I have to extract
From line no. 4 to 272, are the (x,y) coordinate of each node. which also have to be extracted.
kindly help me on extracting these info and placing them in an array variables.
0 Comments
Answers (1)
Paras Gupta
on 20 May 2024
Hello Nandha,
I understand that you want to read the INP file provided in the question and extract the coordiates of the nodes and element nodal connectivity information from the file. The INP file can be opened using the 'fopen' function in MATLAB. The following code shows one way to extract the required information from the INP file:
% Initialize arrays to hold node and element data
nodes = [];
elements = [];
% Open the file for reading
fid = fopen('mesh_doubt1.inp', 'rt');
% Flags to identify the sections
readingNodes = false;
readingElements = false;
while ~feof(fid)
line = strtrim(fgets(fid)); % Read a line and trim whitespace
% Check for the nodes section
if startsWith(line, '*NODE', 'IgnoreCase', true)
readingNodes = true;
readingElements = false;
continue;
end
% Check for the elements section of type CPS4 and ELSET=Surface1
if contains(line, 'type=CPS4', 'IgnoreCase', true) && contains(line, 'ELSET=Surface1', 'IgnoreCase', true)
readingElements = true;
readingNodes = false;
continue;
end
% Check if we are exiting a relevant section
if startsWith(line, '*') && ~startsWith(line, '*NODE') && ~contains(line, 'type=CPS4', 'IgnoreCase', true)
readingNodes = false;
readingElements = false;
end
% Read and store nodes
if readingNodes
nodeData = sscanf(line, '%f, %f, %f, %f')';
nodes = [nodes; nodeData];
end
% Read and store elements of type CPS4 and ELSET=Surface1
if readingElements
elementData = sscanf(line, '%f, %f, %f, %f, %f')';
elements = [elements; elementData];
end
end
% Close the file
fclose(fid);
For more information on the functions used in the above code, you can refer to the documentations links below:
Hope this answers the query.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!