将数据附加到二维数组Netcdf格式Matlab
我有一个函数,它在每次迭代时生成yout_new(5000,1),我想将此数据存储到一个netcdf文件中,并将每次迭代时生成的新数据附加到这个现有文件中。在第二次迭代时,存储的变量大小应为yout_new(5000,2)。这是我的尝试,但不起作用。有什么好办法吗将数据附加到二维数组Netcdf格式Matlab,matlab,netcdf,Matlab,Netcdf,我有一个函数,它在每次迭代时生成yout_new(5000,1),我想将此数据存储到一个netcdf文件中,并将每次迭代时生成的新数据附加到这个现有文件中。在第二次迭代时,存储的变量大小应为yout_new(5000,2)。这是我的尝试,但不起作用。有什么好办法吗 neq=5000; filename='thrust.nc'; if ~exist(filename, 'file') %% cr
neq=5000;
filename='thrust.nc';
if ~exist(filename, 'file')
%% create file
ncid=netcdf.create(filename,'NC_WRITE');
%%define dimension
tdimID = netcdf.defDim(ncid,'t',...
netcdf.getConstant('NC_UNLIMITED'));
ydimID = netcdf.defDim(ncid,'y',neq);
%%define varibale
varid = netcdf.defVar(ncid,'yout','NC_DOUBLE',[ydimID tdimID]);
netcdf.endDef(ncid);
%%put variables from workspace ( i is the iteration)
netcdf.putVar(ncid,varid,[ 0 0 ],[ neq 0],yout_new);
%%close the file
netcdf.close(ncid);
else
%% open the existing file
ncid=netcdf.open(filename,'NC_WRITE');
%Inquire variables
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,0);
varid = netcdf.inqVarID(ncid,varname);
%Enquire current dimension length
[dimname, dimlen] = netcdf.inqDim(ncid,0);
% Append new data to existing variable.
netcdf.putVar(ncid,varid,dimlen,numel(yout_new),yout_new);
netcdf.close(ncid);
MATLAB中有更简单的函数,用于处理netCDF。您阅读了关于ncdisp、ncinfo、nccreate、ncread、ncwrite的信息。说到这个问题,你说你必须写两列,我将列数作为变量(无穷大),每次你都可以附加列。检查以下代码:
N = 3 ; % number of columns
rows = 5000 ; % number of rows
ncfile = 'myfile.nc' ; % my ncfile name
nccreate(ncfile,'yout_new','Dimensions',{'row',rows,'col',Inf},'DeflateLevel',5) ; % creat nc file
% generate your data in loop and write to nc file
for i = 1:N
yout_new = rand(rows,1) ;
ncwrite(ncfile,'yout_new',yout_new,[1,i]) ;
end
请不要这样,列数不受限制不是强制性的,您可以将其改为所需的列数而不是inf。谢谢mate!。。。工作正常…nccreate中是否有其他选项可以覆盖现有文件…目前我正在使用if exist()测试并删除文件。。