I have a .mat file named "myfile.mat" that contains a huge varible data
and, in some cases, another variable data_info
. What is the fastest way to check if that .mat file contains the `data_info' variable?
the who or whos commands are not faster than simply loading and testing for the existens of varible.
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
All methods roughly take the same time (which is way to slow, since data
is huge.
However, this is very fast:
tic
for p=1:nRuns
load('myfile.mat','data_info');
if exist('data_info', 'var')
%do something
end
end
toc
But it issues a warning, if data_info
does not exist. I could suppress the warning, but that doesn't seem like the best way to do this.. What other options are there?
Edit
using who('-file', 'myfile.mat', 'data_info')
is also not faster:
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