将.mat文件写入.nc

时间:2013-10-05 14:31:59

标签: matlab netcdf mat-file

我有一个代码可以创建一堆.mat文件,但我想将它们保存为netcdf文件(csv或txt也可以),所以不能使用MATLAB的人可以访问它们。这就是我到目前为止所拥有的

%% Use function to read in
data = read_mixed_csv(filename,'"'); % Creates cell array of data
data = regexprep(data, '^"|"$',''); % Gets rid of double quotes at the start and end of the string 
data = data(:,2:2:41); % Keep only the even cells because the odd ones are just commas

%% Sort data based on date (Column 1)
[Y,I] = sort(data(:,1)); % Create 1st column sorted
site_sorted = data(I,:); % Sort the entire array

%% Find unique value in the site data (Column 2) 
% Format of site code is state-county-site
u_id = unique(site_sorted(:,2)); % get unique id

for i = 1:length(u_id)
    idx=ismember(site_sorted(:,2),u_id{i}); % extract index where the second column matches the current id value
    site_data = site_sorted(idx,:);
    save([u_id{i} '.mat'],'site_data');
    cdfwrite([u_id{i} '.nc'], 'site_data');
end

一切正常,直到倒数第二行。我想将每个'site_data'写为netcdf文件,其名称与save([u_id{i} '.mat'],'site_data');相同,后者是第二列的字符串。

1 个答案:

答案 0 :(得分:1)

尝试

cdfwrite([u_id{i}],{'site_data',site_data})

扩展名为'.cdf'。我不确定在使用cdfwrite时是否可以更改。

编辑:更正错字