2013-10-05 381 views
0

我有一段代码可以创建一堆.mat文件,但我想将它们保存为netcdf文件(csv或txt也可以),因此无法使用MATLAB的用户可以访问它们。这是我到目前为止的内容将.mat文件写入.nc

%% 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

尝试

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

的分机将是 '.CDF'。我不确定在使用cdfwrite时这是否可以改变。

编辑:更正的错字

+0

您在第二个'site_data'上缺少下划线。但MATLAB理解。谢谢您的帮助。 – shizishan

+0

其实,很抱歉,虽然创建了一个netcdf文件,但它是混乱的混乱。我在想也许是因为变量是一个单元格?你知道如何解决这个问题吗?另外,如何让变量存储在.mat文件u_id {i}中,而不是仅为每个人使用site_data? – shizishan

+0

我会检查一下并回复给你。 –