1 |
function []=write2meta(myFile,myDim,varargin); |
2 |
%purpose: write meta file to match binary file |
3 |
% |
4 |
%inputs: myFile is the file name (must finish with '.data') |
5 |
% myDim is the size of a field (2D or 3D integer vector) |
6 |
%(optional 1) myPrec is the file myPrecision (32, by default, or 64) |
7 |
%(optional 2) fldList is the list of field names (cell array) |
8 |
|
9 |
%precision: |
10 |
if nargin>2; myPrec=varargin{1}; else; myPrec=32; end; |
11 |
if nargin>3; fldList=varargin{2}; else; fldList=[]; end; |
12 |
%number of dimensions |
13 |
nDim=length(myDim); |
14 |
%number of records: |
15 |
tmp1=dir(myFile); |
16 |
nRec=tmp1.bytes/myDim(1)/myDim(2)/myPrec*8; |
17 |
if nDim>2; nRec=nRec/myDim(3); end; |
18 |
if nRec<1|floor(nRec)~=nRec; error('inconsistent dimensions'); end; |
19 |
%check that creating a meta file makes sense w\r\t rdmds |
20 |
if ~strcmp(myFile(end-4:end),'.data'); |
21 |
error('file name must finish in ''.data''\n'); |
22 |
else; |
23 |
myFile=[myFile(1:end-5) '.meta']; |
24 |
end; |
25 |
|
26 |
%create the meta file: |
27 |
fid=fopen(myFile,'wt'); |
28 |
%% |
29 |
fprintf(fid,'nDims = [ %i ];\n',min(nDim,3)); |
30 |
fprintf(fid,' dimList = [\n'); |
31 |
fprintf(fid,' %5i, %5i, %5i,\n',myDim(1),1,myDim(1)); |
32 |
fprintf(fid,' %5i, %5i, %5i,\n',myDim(2),1,myDim(2)); |
33 |
if nDim>2; fprintf(fid,' %5i, %5i, %5i,\n',myDim(3),1,myDim(3)); end; |
34 |
fprintf(fid,' ];\n'); |
35 |
fprintf(fid,' dataprec = [ ''float%2i'' ];\n',myPrec); |
36 |
fprintf(fid,' nrecords = [ %5i ];\n',nRec); |
37 |
if ~isempty(fldList); |
38 |
nFlds=length(fldList); |
39 |
fprintf(fid,' nFlds = [ %i ];\n',nFlds); |
40 |
fprintf(fid,' fldList = {\n'); |
41 |
txt=' '; for ii=1:length(fldList); txt=[txt '''' fldList{ii} '''' ' ']; end; |
42 |
fprintf(fid,[txt '\n']); |
43 |
fprintf(fid,' };\n'); |
44 |
end; |
45 |
%% |
46 |
fclose(fid); |
47 |
|