How to save "char" datatype column data into the NetCDF file along with "int32", "single", and ''double" datatype column data?
3 views (last 30 days)
Show older comments
I have "int32", "single", ''double" and "char" datatype of column data in the attached mat format file (26.8 kb). I want to save all the data into a NetCDF file without changing the size of other variables. But I am getting errors until I am not making the size equal to the variable. Can someone tell me how to put the following four variables into ncfile?
load('gdp_hourly_velocities_f86e_04a9_23f1.mat')
ld1=gdp_hourly_velocities.drogue_lost_date;
lt1=gdp_hourly_velocities.latitude;
rw1=gdp_hourly_velocities.rowsize;
st1=gdp_hourly_velocities.DeploymentStatus;
nccreate('ncfile.nc','ld1','Dimensions',{'ld1',1,size(ld1,1)},'Datatype','double');
ncwrite('ncfile.nc','ld1',ld1);
nccreate('ncfile.nc','lt1','Dimensions',{'lt1',1,size(lt1,1)},'Datatype','single');
ncwrite('ncfile.nc','lt1',lt1);
nccreate('ncfile.nc','rw1','Dimensions',{'rw1',1,size(rw1,1)},'Datatype','int32');
ncwrite('ncfile.nc','rw1',rw1);
nccreate('ncfile.nc','st1','Dimensions',{'st1',4,size(st1,1)},'Datatype','char');
ncwrite('ncfile.nc','st1',st1);
0 Comments
Answers (1)
Divyam
on 17 Sep 2024
Edited: Divyam
on 17 Sep 2024
The dimensions defined in the code are causing the issue since the same variable name is being used when defining a dimension and the NETCDF variable itself. Here's the code for your reference:
% Load the MAT file
load('gdp_hourly_velocities_f86e_04a9_23f1.mat')
% Extract variables
ld1 = gdp_hourly_velocities.drogue_lost_date;
lt1 = gdp_hourly_velocities.latitude;
rw1 = gdp_hourly_velocities.rowsize;
st1 = gdp_hourly_velocities.DeploymentStatus;
% Create the NetCDF file and variables
% 'record' is the name for number of entries of each variable, i.e. 364
nccreate('ncfile.nc', 'ld1', 'Dimensions', {'record', size(ld1, 1)}, 'Datatype', 'double');
ncwrite('ncfile.nc', 'ld1', ld1);
nccreate('ncfile.nc', 'lt1', 'Dimensions', {'record', size(lt1, 1)}, 'Datatype', 'single');
ncwrite('ncfile.nc', 'lt1', lt1);
nccreate('ncfile.nc', 'rw1', 'Dimensions', {'record', size(rw1, 1)}, 'Datatype', 'int32');
ncwrite('ncfile.nc', 'rw1', rw1);
% For the 'char' variables we need to map the char_dim to accurately
nccreate('ncfile.nc', 'st1', 'Dimensions', {'record', size(st1, 1), 'char_dim', size(st1, 2)}, 'Datatype', 'char');
ncwrite('ncfile.nc', 'st1', st1);
For more information regarding the definition of dimensions for a variable while using "nccreate", refer to this documentation: https://www.mathworks.com/help/matlab/ref/nccreate.html?s_tid=doc_ta#:~:text=Dimensions%20of%20the,the%20current%20length.
0 Comments
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!