How to load in variable if it is present in a matfile?

조회 수: 5 (최근 30일)
K E
K E 2015년 7월 21일
댓글: K E 2015년 7월 24일
I am having trouble with "conditional" loading of a variable from a matfile, depending on whether the matfile contains the variable. Here is what I am trying to do:
  1. Go through many different (large) matfiles, some of which contain mountain height in a variable called topo, and some of which contain mountain height in variable called elevation.
  2. If the matfile contains the variable topo, load that into a workspace variable called mountainHeight. If the matfile contains the variable elevation, load that into a workspace variable called mountainHeight. If neither variable is contained in matfile, do nothing.
  3. Avoid crashing the load command by trying to load variables which are not in the matfile.
Here is what I have tried (R2015a), but I'd bet there is a better/shorter way.
filename = 'topography.mat'; % A matfile that comes with matlab
m = matfile(filename); % Make a matfile object
FileInfo = whos(m); % Structure containing information about matfile including variables it contains
for iVar = 1:length(FileInfo)
if strcmp(FileInfo(iVar).name, 'topo') % Check if topo is a variable in topography.mat
mountainHeight = m.topo; % Load topo
elseif strcmp(FileInfo(iVar).name, 'elevation') % Check if elevation is a variable in topography.mat
mountainHeight = m.elevation; % Load elevation
end
end

채택된 답변

Steven Lord
Steven Lord 2015년 7월 21일
D = load('mymatfile.mat');
if isfield(D, 'elevation')
elevation = D.elevation;
else
elevation = NaN;
end
Alternately, check if the field exists and if it doesn't, add it with some placeholder data.
D = load('mymatfile.mat');
if ~isfield(D, 'elevation')
D.elevation = NaN;
end
If you want to do this with multiple fields, use dynamic field names.
D = load('mymatfile.mat');
listOfFields = {'elevation', 'material', 'price'};
isPresent = isfield(D, listOfFields);
for fieldnum = 1:length(listOfFields)
if ~isPresent(fieldnum)
F = listOfFields{fieldnum};
D.(F) = NaN;
end
end
  댓글 수: 7
K E
K E 2015년 7월 23일
Cedric - These matfiles are generated by many different authors and I want to keep track of the originals so I am stuck making my code accommodate the inconsistent names.
Cedric
Cedric 2015년 7월 23일
편집: Cedric 2015년 7월 23일
Then just build a small function that does what Steven proposes, and outputs mountainHeight:
function mountainHeight = loadMountainHeight( locator )
data = load( locator ) ;
if isfield( data, 'topo' )
mountainHeight = data.topo ;
elseif isfield( data, 'elevation' )
mountainHeight = data.elevation ;
else
mountainHeight = [] ; % Or NaN or whatever you want, to tell
% the caller "field not found".
end
end
Then in your main script, you do something like the following:
dir_mat = dir( '*.mat' ) ;
for fId = 1 : numel( dir_mat )
mountainHeight = loadMountainHeight( dir_mat(fId).name ) ;
if isempty( mountainHeight )
% Throw warning, or print message, or just skip.
warning( 'File "%s" : no valid field found!', dir_mat(fId).name ) ;
continue ;
end
fprintf( 'File "%s" : processing height..\n', dir_mat(fId).name ) ;
% .. code for further processing mountainHeight ..
end

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2015년 7월 21일
편집: Sean de Wolski 2015년 7월 23일
Use this to determine what's in the MAT file, then use the MAT File class to load that variable in.
  댓글 수: 1
K E
K E 2015년 7월 24일
Useful to avoid loading whole matfile (they are large).

댓글을 달려면 로그인하십시오.


Cedric
Cedric 2015년 7월 21일
편집: Cedric 2015년 7월 21일
An alternate solution is to catch the warning (LOAD doesn't throw an error).
Or you can turn off this warning and just test if the output struct is empty (at least on 2015b-pre, I can't test on earlier versions now).

카테고리

Help CenterFile Exchange에서 Workspace 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