Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Writing serveal arrays to an axis in a netcdf file

Asked by kenneth86 on 27 Nov 2012

Hi,

I have a 114*150 cell array where each cell contains a vector that varies in size from cell to cell. I want to write these cell arrays to a netcdf file, so that I can correspondingly access each cell in the netcdf file (i.e. solution = netcdf.getVar(ncID,n)). I do not get the vectors I want when doing so. I hope someone with netcdf experience can help me.

Here is my code:

    function obj = dumpNETCDF_NR(obj,Specs)
    depthSize = cell2num(obj.Solutions.cord.z{end});
    offsetSize = cell2num(obj.Solutions.cord.x{end});
    ncID = netcdf.create(Specs.solutionFilePath,'NC_SHARE');
    %Define dimensions
    dimID_Solutions = netcdf.defDim(ncID,'PLF',netcdf.getConstant('NC_UNLIMITED'));
    dimID_Depth = netcdf.defDim(ncID,'Depth',depthSize);
    dimID_Offset = netcdf.defDim(ncID,'Offset',offsetSize);
    %Define axis for depth
    varID_Depth = netcdf.defVar(ncID,'Depth','double',dimID_Depth);
    netcdf.putAtt(ncID,varID_Depth,'long_name','Depth');
    netcdf.putAtt(ncID,varID_Depth,'units','Meters*10^1');
    %Define axis for offset
    varID_Offset = netcdf.defVar(ncID,'Offset','double',dimID_Offset);
    netcdf.putAtt(ncID,varID_Offset,'long_name','Offset');
    netcdf.putAtt(ncID,varID_Offset,'units','Meters*10^1');
% Define soltuions(PLF)axis: 
varID_PLF = netcdf.defVar(ncID,'Porosity','double',dimID_Solutions);     netcdf.putAtt(ncID,varID_PLF,'long_name','Porosity'));
netcdf.putAtt(ncID,varID_PLF,'units','Volume fractions'));
netcdf.putAtt(ncID,varID_PLF,'missing_value',-9999);
netcdf.endDef(ncID)    
% Write solutions to netCDF:        
for x = 1:offsetSize
    for z = 1:depthSize
%Write solutions            netcdf.putVar(ncID,varID_PLF,0,numel(obj.Solutions.Prop3.Porosity{z}{x}),...
                  obj.Solutions.Prop3.Porosity{z}{x})
      end
  end
    %% Write depth and offset values to netCDF
    netcdf.putVar(ncID,varID_Depth,1:depthSize);
    netcdf.putVar(ncID,varID_Offset,1:offsetSize);
    %% Read the netCDF file
    netcdf.close(ncID)
    obj.ncID = {};
    obj.ncID = netcdf.open(fullfile(Specs.solutionFilePath),'NC_SHARE');
    netcdf.close(obj.ncID)

Best regards, Kenneth

1 Comment

Ashish Uthama on 4 Jan 2013

Any reason this has to be a netcdf file? The data doesnt look well suited for this format. HDF5 might be more suitable perhaps.

kenneth86

Products

No products are associated with this question.

0 Answers

Contact us