快速检查变量是否在.mat文件中而不加载.mat文件? 'who'/'whos'并不比loading更快..比'who'更好的选择?

时间:2017-08-01 15:40:19

标签: matlab variables mat-file

我有一个名为“myfile.mat”的.mat文件,其中包含一个巨大的变量data,在某些情况下还包含另一个变量data_info。检查.mat文件是否包含`data_info'变量的最快方法是什么?

who或whos命令并不比简单地加载和测试varible exists更快。

nRuns=10;
%simply loading the complete file
tic
for p=1:nRuns
    load('myfile.mat');
    % do something with variable
    if exist('data_info','var')
        %do something
    end
end
toc

% check with who
tic
for p=1:nRuns
   variables=who('-file','myfile.mat');
   if ismember('data_info', variables)
       % do something
   end
end
toc

% check with whose
tic
for p=1:nRuns
   info=whos('-file','myfile.mat');
   if ismember('data_info', {info.name})
       %do something
   end
end
toc

所有方法大致需要相同的时间(由于data很大,所以这种方法很慢。

然而,这非常快:

tic
for p=1:nRuns
    load('myfile.mat','data_info');
    if exist('data_info', 'var')
        %do something
    end
end
toc

但如果data_info不存在,它会发出警告。我可以抑制警告,但这似乎不是最好的方法。还有什么其他选择?

修改 使用who('-file', 'myfile.mat', 'data_info')也不会更快:

tic
for p=1:nRuns
    if ~isempty(who('-file', 'myfile.mat', 'data_info'))
      % do something
    end
end
toc    % this takes 7 seconds, roughly the same like simply loading complete .mat file

3 个答案:

答案 0 :(得分:3)

尝试使用who将其限制为仅限于特定变量:

...
if ~isempty(who('-file', 'myfile.mat', 'data_info'))
  %do something
end

定时解决方案:

在不同的解决方案(下面包含的代码,在Windows 7和MATLAB版本R2016b上运行)上使用timeit表明基于who的解决方案看起来最快,我上面提到的那个有轻微的速度快。这是时间,从最慢到最快:

Load whole file:        0.368235871921381 sec
Using matfile:          0.001973860748417 sec
Load only `data_info`:  0.000316989486384 sec
Using whos + ismember:  0.000174207817967 sec
Using who + ismember:   0.000151289605527 sec
Using who + isempty:    0.000137261391331 sec

我使用了包含以下变量的示例MAT文件:

data = ones(10000);
data_info = 'hello';

这是测试代码:

function T = infotest

  T = zeros(6, 1);
  T(1) = timeit(@use_load_exist_1);
  T(2) = timeit(@use_load_exist_2);
  T(3) = timeit(@use_matfile);
  T(4) = timeit(@use_whos_ismember);
  T(5) = timeit(@use_who_ismember);
  T(6) = timeit(@use_who_isempty);

end

function isThere = use_load_exist_1
  load('infotest.mat');
  isThere = exist('data_info', 'var');
end

function isThere = use_load_exist_2
  load('infotest.mat', 'data_info');
  isThere = exist('data_info', 'var');
end

function isThere = use_matfile
  isThere = isprop(matfile('infotest.mat'), 'data_info');
end

function isThere = use_whos_ismember
  info = whos('-file', 'infotest.mat');
  isThere = ismember('data_info', {info.name});
end

function isThere = use_who_ismember
  variables = who('-file', 'infotest.mat');
  isThere = ismember('data_info', variables);
end

function isThere = use_who_isempty
  isThere = ~isempty(who('-file', 'infotest.mat', 'data_info'));
end

答案 1 :(得分:1)

您可以使用who命令https://www.mathworks.com/help/matlab/ref/who.html

这样做的语法是使用文件的指示符调用who,然后调用您要查找的变量。您不需要查找文件中的所有变量

虚拟语法如下

variable = who('-file','yourfilenamehere','data_info')

从那里你可以打电话

if ~isempty(variable)
%do something
end

仅搜索文件中的该变量。在您的who命令版本中,您查找了所有变量,而这只是查找一个变量。

答案 2 :(得分:0)

所以它有点凌乱,但我只是尝试了这个,无论大小如何,它都非常即时。让我知道它是否适合您。

请原谅格式,我不习惯这里的正确格式。

注意:此解决方案使用已内置于 matlab 中的低级 HDF5 库,因此此方法假定您的 mat 文件为 HDF5 (-v7.3)。否则将无法工作。

您可以通过执行以下操作确定是一个有效的 hdf5 文件:

isValidHDF = H5F.is_hdf5('my_file.mat');

要查看您的变量是否存在:

isThere = false; %Initialize as default value of false
fid = H5F.open('myfile.mat') % Use low level H5F builtin to open
try % Never use try/catch but this is a good for when its ok
     % Try to open the h5 group. Will error and catch to report back false if the variable isnt there, otherwise the variable exists
     gid = H5G.open(fid,['/data_info']); % Note: the "/" is required and OS independent, so its never "\" even in windows

     % I think this makes sure the variable isnt empty if the group opened successfully, but it hasnt been a problem yet
     hInfo = H5G.get_info(gid); 
     isThere = hInfo.nlinks > 0;
     H5G.close(gid);
end
H5F.close(fid);