Code covered by the BSD License  

Highlights from
saves variables into MAT file matfile

saves variables into MAT file matfile

by

 

saves variables into MAT file matfile

setfile(matfile,varargin)
function setfile(matfile,varargin)
%% setfile saves variables into MAT file matfile.
% example: setfile('temp.mat','a',magic(5));
% if matfile does not exist, setfile creates matfile and save variables.
% if matfile exists, setfile has the basic usage same as save
%
% Version: 2/20/2013.
% Chao-Wei Chen

if nargin==0 || ~exist('matfile','var') || ~ischar(matfile),
    error('setfile(matfile,varargin)');
end


num=numel(varargin);
switch(rem(num,2))
    case 0
        while(~isempty(varargin))         
            varname=varargin{1};
            if ischar(varname),
                save_routine(matfile,varname,varargin{2});                
            else
                disp(varname);
                disp('...is skipped')
            end
            varargin=varargin(3:end);
        end
    case 1,error('varargin must be paired');
end
end

function tf=is_matfile_exist(matfile)

[~,~,ext]=fileparts(matfile);
if isempty(ext), matfile=[matfile '.mat'];end

tf=~isempty(which(matfile));
end

function save_routine(matfile,varname,varcontent)
eval([varname '=varcontent;']);
if is_matfile_exist(matfile),
    save(matfile,'-append','-v7.3',varname);
else
    save(matfile,'-v7.3',varname);
end
end

Contact us