당신이 물어 본 것은 정확하지 않지만, 구조의 모든 {double, single, char} 변수를 netcdf 파일에 쓰는 스크립트가 있습니다. 3D 변수까지만 처리하기 때문에 가장 일반적인 코드는 아닙니다 (확장하기는 쉽지만). 내가 일반적인 사용과 관심이 될 수 있다고 생각하기 때문에 여기에 게시하고 있습니다. 아마도 누군가는 더 나은 버전을 게시 할 것입니다.
또한 스칼라 또는 char이 아닌 모든 변수를 압축합니다.
function struct2nc(x,ncfile,ncfiletype,deflate_lev)
% STRUCT2NC writes all float,double and character vars to netcdf
% Usage: struct2nc(x,ncfile,[ncfiletype],[deflate_lev])
% x = structure
% ncfile = name of netcdf output file (e.g. 'test.nc')
% ncfiletype = netcdf file type (e.g. 'classic','netcdf4_classic')
% deflate_lev = deflate level (0-9, 0 is none)
%
% This function writes all 'double','single' and 'char' variables
% to NetCDF using the native Matlab NetCDF interface. It skips all
% other classes in the struct (e.g. structs, cell arrays, etc). It
% also only handles scalar, 1D, 2D, and 3D arrays currently, although
% this could easily be extended.
if nargin==2,
ncfiletype='classic';
deflate_lev=0;
elseif nargin==3;
switch ncfiletype
case {'netcdf4','netcdf4_classic'}
deflate_lev=6;
otherwise
deflate_lev=0;
end
end
s = fieldnames(x);
k=0;
% create variables first, but don't write data
for i=1:length(s)
vname=char(s(i));
var=x.(vname);
vtype = class(var);
vshape = size(var);
ndims = length(vshape);
vlen = length(var(:));
switch vtype;
case {'double','single'},
if vlen==1,
nccreate(ncfile,vname,...
'Datatype',vtype,'format',ncfiletype);
k=k+1;
vnames{k}=vname;
else
if min(vshape)==1,
nccreate(ncfile,vname,...
'Datatype',vtype,...
'DeflateLevel',deflate_lev,...
'Dimensions',{[vname '1'] vlen},...
'format',ncfiletype);
k=k+1;
vnames{k}=vname;
elseif ndims==2,
nccreate(ncfile,vname,...
'Datatype',vtype,...
'DeflateLevel',deflate_lev,...
'Dimensions',{[vname '1'] vshape(1) [vname '2'] vshape(2)},...
'format',ncfiletype);
k=k+1;
vnames{k}=vname;
elseif ndims==3,
nccreate(ncfile,vname,...
'Datatype',vtype,...
'DeflateLevel',deflate_lev,...
'Dimensions',...
{[vname '1'] vshape(1) [vname '2'] vshape(2) [vname '3'] vshape(3)},...
'format',ncfiletype);
k=k+1;
vnames{k}=vname;
else,
disp('Skipping variable with more than 3 dimensions');
end
end
case {'char'},
nccreate(ncfile,vname,...
'Datatype',vtype,...
'Dimensions',{[vname '1'] vlen},.....
'format',ncfiletype);
k=k+1;
vnames{k}=vname;
otherwise,
disp(['skipping ' vname])
end
end
%write all the data at the end
for i=1:length(vnames)
ncwrite(ncfile,vnames{i},x.(vnames{i}));
end
관련 항목 : [cell2struct] (http://www.mathworks.com/help/matlab/ref/cell2struct.html) (Matlab 버전에 따라 다름) – ZZZ