Code covered by the BSD License  

Highlights from
loads one variable from MAT-file(s) into workspace

loads one variable from MAT-file(s) into workspace

by

 

Need to load a same variable from a list of MAT files? This is for you!

[vo,voname,matname]=getvar(varargin)
function [vo,voname,matname]=getvar(varargin)
%% getvar loads one variable from MAT-file(s) into workspace
%
%% [vo,voname,matname]=getvar popups GUI to ask which MAT file(s) to load 
% and load what variable. vo is the content of the loaded variable and 
% whose name can be assigned by user. voname is the original variable name. 
% matname is the MAT file(s) being loaded.
% If more than one file is loaded, vo is a cell containing the
% content of the variable from each file. matname is a cell containing the
% MAT file list
%% [...]=getvar(matname) loads one variable from MAT file(s) matname.

%% [...]=getvar(varnamefilter) asks which MAT file(s) to load and use
% varnamefilter to prefilter the name of variables that match the specified
% regular expressions.
%
%% [...]=getvar(matname,varnamefilter) loads one variable filtered by
% varnamefilter from MAT file(s) fnames into vo and voname.
switch(nargin)    
    case 0,     
        matname=get_cflist;
        [vo,voname,matname]=getvar(matname);        
    case 1, % accept struct or filename string(s) in a cell
        vi=varargin{1};                
        switch(class(vi))
            case 'cell'
                matname=vi;[vo,voname]=getvar_from_files(matname,'.*');                
            case 'char'
                if isfile(vi)
                    matname={vi};
                    [vo,voname]=getvar_from_files(matname,'.*');
                else
                    varnamefilter=vi;matname=get_cflist;
                    [vo,voname]=getvar_from_files(matname,varnamefilter);
                end
            case 'struct' % unofficial usage
                si=vi;[vo,voname]=gervar_from_struct(si);
                matname=voname; % structure fieldname
        end                
    case 2, % accept only filename string(s)
        vi=varargin{1};varnamefilter=varargin{2};
        switch(class(vi))
            case 'cell'
                matname=vi;[vo,voname]=getvar_from_files(matname,varnamefilter);
            case 'char'
                matname={vi};[vo,voname]=getvar_from_files(matname,varnamefilter);
        end  
    otherwise,        
        error('<a href="matlab:help(''getvar'')">see getvar usage</a>')
end

end

function cflist=get_cflist
    [cflist,p]=uigetfile('*.mat','select MAT files','multiselect','on');
    if p==0,error('Empty file list');end    
    if ~iscell(cflist),cflist={cflist};end
    for k=1:numel(cflist)
        cflist{k}=fullfile(p,cflist{k});
    end
end

function [vo,fieldname]=gervar_from_struct(vi,fieldname)
% unofficial support
if nargin<2,
    fieldlist=fieldnames(vi);
    if numel(fieldlist)==1,
        fieldname=fieldlist{1};
    else
        fieldname=fieldlist{menu('which field?',fieldlist )};
    end
end
vo=vi.(fieldname);
end

function [vo,voname]=getvar_from_files(cflist,varnamefilter)
vo=cell(size(cflist));
[vo{1},voname]=getvar_from_onefile(cflist{1},varnamefilter);
if numel(cflist)==1,
    vo=vo{1};
else
    for k=2:numel(cflist)
        vo{k}=getvar_from_onefile(cflist{k},voname);    
    end
end
end
function [vo,voname]=getvar_from_onefile(fname,varnamefilter)
assert(isfile(fname),[fname ' not found']);
[~,~,ext]=fileparts(fname);

cimg_ext_supported=imformats;
cimg_ext_supported={cimg_ext_supported(:).ext};
switch(lower(ext))
    case '.mat',        
        [vo,voname]=getvar_from_matfile(fname,varnamefilter);
    case {'.tif','.tiff'} % unofficial support
        vo=tif2mat(fname);voname=fname;  
    case cimg_ext_supported, % unofficial support
        vo=imread(fname);voname=fname;
end
end

function [var,varname]=getvar_from_matfile(matname,varnamefilter)

vars=whos('-file',matname,'-regexp',['\<' varnamefilter '\>']);
num_vars=numel(vars);

switch(num_vars)
    case 0
        disp({[vars(:).name]});
        error([ varnamefilter ' is not found.']);        
    case 1
        varname=vars(1).name;
        s=load(matname,varname);
        var=s.(varname);
    otherwise
        vars_name=cell(num_vars,1);
        for k=1:num_vars,
            vars_name(k)={vars(k).name};    
        end
        select=menu('choose a single var:',vars_name);
        assert(select>0,'no variable selected');
        varname=vars_name{select};
        s=load(matname,varname);
        var=s.(varname);   
end
end

function tf=isfile(fname)
tf=~isempty(which(fname));
end

Contact us