0001 function savejmesh(node,face,elem,fname,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 if(nargin==2)
0028 fname=face;
0029 face=[];
0030 elem=[];
0031 end
0032
0033 if(nargin==3)
0034 fname=elem;
0035 elem=[];
0036 end
0037
0038 if(length(varargin)==1 && ischar(varargin{1}))
0039 opt=struct('FileName',varargin{1});
0040 else
0041 opt=varargin2struct(varargin{:});
0042 end
0043
0044 meshdim=jsonopt('Dimension',0,opt);
0045
0046 fid=fopen(fname,'wt');
0047 if(fid==-1)
0048 error('You do not have permission to save mesh files.');
0049 end
0050
0051 mesh.MeshVersion=0.5;
0052 mesh.CreationTime=datestr(now);
0053 mesh.Comment=['Created by iso2mesh ' iso2meshver '(http://iso2mesh.sf.net)'];
0054
0055 if(meshdim==0)
0056 mesh.MeshNode=node;
0057 if(~isempty(face))
0058 mesh.MeshSurf=face;
0059 end
0060 if(~isempty(elem))
0061 mesh.MeshElem=elem;
0062 end
0063 elseif(meshdim==3)
0064 nd=size(node);
0065 if(nd(2)<3) error('expecting 3 or more columns in node'); end
0066 mesh.MeshPoint3D=node(:,1:3);
0067 if(nd(2)>3)
0068 mesh.MeshNodeVal=node(:,4:end);
0069 end
0070 if(~isempty(face))
0071 nd=size(face);
0072 if(nd(2)<3) error('expecting 3 or more columns in face'); end
0073 mesh.MeshTri=face(:,1:3);
0074 if(nd(2)>3)
0075 mesh.MeshTriVal=face(:,4:end);
0076 end
0077 end
0078 if(~isempty(elem))
0079 nd=size(elem);
0080 if(nd(2)<4) error('expecting 4 or more columns in elem'); end
0081 mesh.MeshTetra=elem(:,1:4);
0082 if(nd(2)>4)
0083 mesh.MeshTetraVal=elem(:,5:end);
0084 end
0085 end
0086 elseif(meshdim==2)
0087 nd=size(node);
0088 if(nd(2)<2) error('expecting 2 or more columns in node'); end
0089 mesh.MeshPoint2D=node(:,1:2);
0090 if(nd(2)>2)
0091 mesh.MeshNodeVal=node(:,3:end);
0092 end
0093 if(~isempty(face))
0094 nd=size(face);
0095 if(nd(2)<3) error('expecting 3 or more columns in face'); end
0096 mesh.MeshTri=face(:,1:3);
0097 if(nd(2)>3)
0098 mesh.MeshTriVal=face(:,4:end);
0099 end
0100 end
0101 if(~isempty(elem))
0102 warning('elem is redundant in a 2D mesh, skip');
0103 end
0104 else
0105 error('the specified Dimension is not supported, please remove to save data to a general format');
0106 end
0107
0108 author=jsonopt('Author','',opt);
0109 if(~isempty(author))
0110 mesh.Author=author;
0111 end
0112
0113 title=jsonopt('MeshTitle','',opt);
0114 if(~isempty(title))
0115 mesh.MeshTitle=title;
0116 end
0117
0118 tag=jsonopt('MeshTag',[],opt);
0119 if(~isempty(tag))
0120 mesh.MeshTag=tag;
0121 end
0122
0123 group=jsonopt('MeshGroup',[],opt);
0124 if(~isempty(group))
0125 mesh.MeshTag=group;
0126 end
0127
0128 comm=jsonopt('Comment','',opt);
0129 if(~isempty(comm))
0130 mesh.Comment=comm;
0131 end
0132
0133 fprintf(fid,'%s\n',savejson('',mesh,varargin{:}));
0134
0135 fclose(fid);