2013-10-05 56 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

我會檢查一下並回復給你。 –