function H = spm_eeg_history(S) % Generate a MATLAB script from the history of an M/EEG SPM data file % FORMAT H = spm_eeg_history(S) % % S - filename or input struct (optional) % (optional) fields of S: % history - history of M/EEG object (D.history) % sname - filename of the MATLAB script to generate % % H - cell array summary of history for review purposes %__________________________________________________________________________ % % In SPM for M/EEG, each preprocessing step enters its call and input % arguments into an internal history. The sequence of function calls that % led to a given file can be read by the history method (i.e. call % 'D.history'). From this history this function generates a script (m-file) % which can be run without user interaction and will repeat, if run, the % exact sequence on the preprocessing steps stored in the history. Of % course, the generated script can also be used as a template for a % slightly different analysis or for different subjects. %__________________________________________________________________________ % Copyright (C) 2008-2015 Wellcome Trust Centre for Neuroimaging % Stefan Kiebel % $Id: spm_eeg_history.m 6890 2016-09-28 13:34:47Z vladimir $ try h = S.history; catch [D, sts] = spm_select(1, 'mat', 'Select M/EEG mat file'); if ~sts, H = {}; return; end D = spm_eeg_load(D); h = D.history; end if nargout H = hist2cell(h); else try S.sname; % not fname, as it means something else for MEEG object catch [filename, pathname] = uiputfile('*.m', ... 'Select the to be generated script file'); if isequal(filename,0) || isequal(pathname,0) return; end S.sname = fullfile(pathname, filename); end hist2script(h,S.sname); end %========================================================================== % function hist2script %========================================================================== function hist2script(h,fname) histlist = convert2humanreadable(h); [selection, ok]= listdlg('ListString', histlist, 'SelectionMode', 'multiple',... 'InitialValue', 1:numel(histlist) ,'Name', 'Select history entries', ... 'ListSize', [400 300]); if ok, h = h(selection); else return; end Nh = length(h); fp = fopen(fname, 'wt'); if fp == -1 error('File %s cannot be opened for writing.', fname); end fprintf(fp, '%s\n\n', 'spm(''defaults'', ''eeg'');'); for i = 1:Nh fprintf(fp, '%s\n', 'S = [];'); s = gencode(h(i).args(1), 'S'); for j = 1:length(s) fprintf(fp, '%s\n', s{j}); end fprintf(fp, '%s\n\n\n', ['D = ' h(i).fun '(S);']); end fclose(fp); %========================================================================== % function hist2cell %========================================================================== function H = hist2cell(h) nf = length(h); H = cell(nf,4); for i=1:nf H{i,1} = char(convert2humanreadable(h(i))); H{i,2} = h(i).fun; args = h(i).args; try,args = args{1};end switch h(i).fun case 'spm_eeg_convert' H{i,3} = args.dataset; if i