【问题标题】:Writing .mat files to .nc将 .mat 文件写入 .nc
【发布时间】:2013-10-12 11:51:46
【问题描述】:

我有一个创建一堆 .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' 写成一个与save([u_id{i} '.mat'],'site_data'); 同名的 netcdf 文件,这是来自第二列的字符串。

【问题讨论】:

    标签: matlab netcdf mat-file


    【解决方案1】:

    试试

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

    扩展名将是“.cdf”。我不确定这是否可以在使用 cdfwrite 时更改。

    编辑:纠正错字

    【讨论】:

    • 您在第二个“site_data”上缺少下划线。 MATLAB虽然理解。感谢您的帮助。
    • 其实很抱歉,虽然创建了一个 netcdf 文件,但它是乱七八糟的。我在想可能是因为变量是一个单元格?你知道如何解决这个问题吗?另外,我如何将变量存储在 .mat 文件 u_id{i} 中,而不仅仅是每个变量的 site_data?
    • 我会检查并回复您。
    • 我可以用 cdfread 毫无问题地打开我用这段代码创建的 cdf 文件。如果您的意思是要存储更多变量,那么您只需像这样添加变量:cdfwrite([u_id{i}],{'site_data',site_data,'nextVarName',nextVarValue,...,'LastV‌​arName',lastVarValue});
    • 当我尝试 cdfread 时,我只得到一列所有数据都下降了。我怎样才能保持原来的格式?我正在使用的 csv 文件可以在这里找到:dropbox.com/sh/li3hh1nvt11vok5/4YGfwStQlo
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2011-08-06
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多