ONLY load matfiles that contain a variable

2 ビュー (過去 30 日間)
James hall
James hall 2013 年 4 月 22 日
Hi Guys,
I want to detect if a .mat file contains a variable before the script loads it. for the script that I am writing I only want one data set and there are 8297 files to check, so to save time and prevent crashes I only want to load faultsActiveList. However because there are not always faults active it is not always present.
DATA = load(MATfile,'faultsActiveList')
please bear in mind allthough I am the author of many of the scripts I use I do use some that others in my company wrote and they are p.coded so I cannot effect how the data comes to me.
Many Thanks James

採用された回答

Friedrich
Friedrich 2013 年 4 月 22 日
編集済み: Friedrich 2013 年 4 月 22 日
Hi,
you can use whos to see if there are any variables in the mat file and which ones:
info = whos('-file','myfile.mat')
info will be a structure of size nx1 where n is the amount of variables in the matfile. In addition the field name will tell you the names of the variables in the mat file.
So you can do
info = whos('-file','myfile.mat')
if any(strcmp({info.name},'faultsActiveList'))
load('myfile.mat','faultsActiveList')
end
  1 件のコメント
James hall
James hall 2013 年 4 月 23 日
Thanks very much Friedrich, works perfectly

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 4 月 22 日
if isempty(who('faultsActiveList', '-file', MATfile))
... not there ...
end

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by