保存.mat文件时,是否使用-v7.3开关?

4

我正在运行一个m文件,创建两个变量,ClusWatts_Map。 我想将这两个变量保存到以“.mat”结尾的文件中。 这两个变量的维度为(1152,241,319),其中1152表示在0.3125度增量下的360度经度,241表示从南纬30度到北纬30度的0.25度增量的纬度,共319个时间步长。 代码一直运行到最后,但出现以下错误:

[Warning: Variable 'Clus' cannot be saved to a MAT-file whose version is older
than 7.3.
To save this variable, use the -v7.3 switch.
Skipping...] 
[Warning: Variable 'Watts_Map' cannot be saved to a MAT-file whose version is
older than 7.3.
To save this variable, use the -v7.3 switch.
Skipping...] 

我正在使用Matlab版本R2014a,因此认为这是最新的版本。此外,我已经在较小的空间域上运行了完全相同的代码(但在2920个时间步骤内),没有出现错误。

%  Clear all variables, initialize counter, indexed by timestep
clc;
clear all;

rain = NaN(1152,241,319);
Clus = NaN(1152,241,319);
Areas_Final = NaN(500,319); 
Wattage_Final = NaN(500,319);
Cluster_size = zeros(319,1);
Watts_Map = zeros(1152,241,319);

for year = 2000%:2008;
    Nyear = sprintf('%04d',year);

    %  Call on the files for each year
    filename = (['pr_3hr_GFDL-HIRAM-C360_amip_r1i1p1_' num2str(Nyear) '010100-' num2str(Nyear) '123123_subset_TROPICS.nc']);
    disp(filename)
    disp(year)

    rain_rate = ncread(filename,'pr');

    %  Call on each timestep
    for i = 960:4:2236; % Approx May 1st-Sep 30th

        %  Set extract subset for region, mask land areas, for each
        %  timestep

        disp(i)
        rain = rain_rate(:,:,i);

        % Eliminate bad/no data points
        index_rain = (rain >= (5.4e-05)) & (rain < 1e-2); % 0.2mm/hr is min rain rate

        % Cluster each morning and afternoon matrix
        Clus(:,:,i) = cluster_it(index_rain);

        % Calculate cluster areas
        Areas = cluster_areas_TROPICS(Clus(:,:,i));
        Areas_Final(1:length(Areas),i) = Areas;

        % Calculate cluster wattages
        Wattage = cluster_wattage(Clus(:,:,i),rain);

        Cluster_size(i,1) = max(max(Clus(:,:,i)));

        % Create dummy matricies to populate and use to create the wattage
        % maps
        D = zeros(1152,241);
        E = squeeze(Clus(:,:,i));
        for index = 1:Cluster_size(i);
            D(E == index) = Wattage(index);
        end

        Watts_Map(:,:,i) = D;

        % Clear the dummy matricies
        clear D E

    end

    % Save the output as a .mat file
    file_out = sprintf(num2str(Nyear), year);
    matfile = [file_out '_TROPICS_Watts_Maps_inc_Land_low_rain_threshold.mat'];
    save(matfile, 'Clus', 'Watts_Map');

    % Clear unneeded variables to save memory and prevent overwriting
    clear index_rain rain Areas Wattage Clus file_out filename Areas_Final rain_rate Watts_Map Cluster_size year matfile

end
1个回答

9
即使在当前版本中, 默认格式也不是v7.3。您必须指定它:
save(matfile, '-v7.3', 'var1', 'var2');

您也可以在“常规偏好设置”中设置默认值:

enter image description here

请注意v7.3在R2008a版本之后可以压缩数据:

压缩-v7.3 MAT-Files

使用save函数的-v7.3选项,您可以将大小超过2 GB的数据项存储在MAT-file中。该选项在MATLAB R2006b中引入。从MATLAB R2008a开始,save现在会压缩这些MAT-files。

我曾看到v7.3文件的文件大小增长,但那已成为历史,也许现在情况更好了。


1
v7.3只是稍微修改了HDF5格式 - HDF5可以使用压缩,快速测试表明MATLAB确实压缩了数据。对我来说,相同的数据保存在v6、v7和v7.3中,分别产生大小为14MB、989KB和943KB的文件。这表明v7.3不仅可以压缩数据,有时还能打败v7的压缩。至于为什么MATLAB不默认使用v7.3,可能与复杂嵌套结构/单元数组的文件大小有点失控有关。 - MrAzzaman
@MrAzzaman 很高兴知道它可以压缩。以前它不能压缩,这是一个很大的缺点。根据发布说明,看起来是在R2008a版本中实现的。 - chappjc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接