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');
相同,它是第二列的字符串。
您在第二个'site_data'上缺少下划线。但MATLAB理解。谢谢您的帮助。 – shizishan
其实,很抱歉,虽然创建了一个netcdf文件,但它是混乱的混乱。我在想也许是因为变量是一个单元格?你知道如何解决这个问题吗?另外,如何让变量存储在.mat文件u_id {i}中,而不是仅为每个人使用site_data? – shizishan
我会检查一下并回复给你。 –