Matlab 快速检查变量是否在.mat文件中而不加载.mat文件的方法';世卫组织'/';谁';不比加载快。。比'更好的选择;谁;?
我有一个名为“myfile.mat”的.mat文件,其中包含一个巨大的变量Matlab 快速检查变量是否在.mat文件中而不加载.mat文件的方法';世卫组织'/';谁';不比加载快。。比'更好的选择;谁;?,matlab,variables,mat-file,Matlab,Variables,Mat File,我有一个名为“myfile.mat”的.mat文件,其中包含一个巨大的变量data,在某些情况下,还包含另一个变量data\u info。检查.mat文件是否包含“data\u info”变量的最快方法是什么 who或whos命令并不比简单地加载和测试变量的存在更快 nRuns=10; %simply loading the complete file tic for p=1:nRuns load('myfile.mat'); % do something with variab
data
,在某些情况下,还包含另一个变量data\u info
。检查.mat文件是否包含“data\u info”变量的最快方法是什么
who或whos命令并不比简单地加载和测试变量的存在更快
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
所有的方法都需要大致相同的时间(这是一种缓慢的方式,因为数据
非常庞大)
然而,这是非常快的:
tic
for p=1:nRuns
load('myfile.mat','data_info');
if exist('data_info', 'var')
%do something
end
end
toc
但是如果数据信息不存在,它会发出警告。我可以抑制警告,但这似乎不是最好的方法。还有什么其他选择
编辑
使用who('-file',myfile.mat',data\u 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
尝试将其限制为仅特定变量:
...
if ~isempty(who('-file', 'myfile.mat', 'data_info'))
%do something
end
解决方案的时间安排:
使用不同的解决方案(下面包含的代码,在Windows 7和MATLAB版本R2016b上运行)表明,基于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
您可以使用
who
命令
其语法是使用文件的指示符调用who
,然后调用您要查找的变量。您不需要查找文件中的所有变量
伪语法如下
variable = who('-file','yourfilenamehere','data_info')
从那里你可以打电话
if ~isempty(variable)
%do something
end
这只搜索文件中的变量。在您版本的
who
命令中,您查找了所有变量,而这只查找了一个。因此有点混乱,但我只是尝试了这个方法,无论大小,它几乎都是即时的。请告诉我它是否适用于您
请原谅格式化,我不习惯这里的正确格式
注意:此解决方案使用已内置在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);
也许?请注意,对于正确的计时,
tic/toc
是不准确的;最好改用它。@excaza:这比加载整个文件要好,但仍然不如whos/who
好。请参见我的答案中的计时结果。不,这比加载完整的.mat文件快不了多少。@AntonRodenhauser:您是如何测量的g那?timeit
比tic/toc
更准确。在所有情况下,它大约需要7秒。即使timeit稍微准确一点——我想它不会有太大的区别?@AntonRodenhauser:使用timeit
我发现使用whos/who
和加载数据之间存在显著差异(尤其是加载所有文件)。您使用的是什么版本的MATLAB?不,这并不比加载完整的.mat文件快