How to save "char" datatype column data into the NetCDF file along with "int32", "single", and ''double" datatype column data?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Divyam
2024-9-17
编辑:Divyam
2024-9-17
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!