%get the directory of your input files: pathname = fileparts('/input/file'); %use that when you save matfile = fullfile(pathname, 'output.mat'); figfile = fullfile(pathname, 'output.fig'); save(matfile, ...'); saveas(figfile, ...');