save_erps() - Saves a GND or GRP variable to disk using its current filename or a newly specified filename. Usage: >> erp_var=save_erps(erp_var,new_fname,new_fname_path); Required Inputs: erp_var - a Mass Univariate ERP Toolbox GND or GRP struct variable Optional Inputs: new_fname - ['gui' or a filename] If the string 'gui,' then a GUI will be created that you can use to pick a filename. Otherwise, new_fname should be a string indicating the new filname that will be used to save the variable (by convention this should NOT include the file path and should end in the extension "GND" or "GRP"). If not specified, the filename and path currently stored in the GND/GRP variable will be used. new_fname_path - a string indicating the new filname path that will be used to save the variable. If not specified and a new filename is specified, the current working directory will be used. force - [integer] If 0, user will be prompted to verify filename before saving. {default: 0} Outputs: erp_var - Same as input GND/GRP variable, except filename information is updated (if a new filename and/or path were specified) and GND/GRP.saved field is set to 'yes.' Also, file is saved to disk. Note: -For some reason, the GUI doesn't work properly on Macs. Specifically, the file selection based on file extension doesn't work. Examples: To save a GND variable with the GUI: >>GND=save_erps(GND,'gui'); To save a GND variable to a new filename: >>GND=save_erps(GND,'yngvob.GND'); To save a GRP variable using the filename and filepath currently stored in the GRP variable: >>GRP=save_erps(GRP); Author: David Groppe Kutaslab, 3/2010
0001 function erp_var=save_erps(erp_var,new_fname,new_fname_path,force) 0002 % save_erps() - Saves a GND or GRP variable to disk using its current filename 0003 % or a newly specified filename. 0004 % 0005 % Usage: 0006 % >> erp_var=save_erps(erp_var,new_fname,new_fname_path); 0007 % 0008 % Required Inputs: 0009 % erp_var - a Mass Univariate ERP Toolbox GND or GRP struct variable 0010 % 0011 % Optional Inputs: 0012 % new_fname - ['gui' or a filename] If the string 'gui,' then a GUI 0013 % will be created that you can use to pick a filename. 0014 % Otherwise, new_fname should be a string indicating the 0015 % new filname that will be used to save the variable (by 0016 % convention this should NOT include the file path and 0017 % should end in the extension "GND" or "GRP"). If not 0018 % specified, the filename and path currently stored in 0019 % the GND/GRP variable will be used. 0020 % new_fname_path - a string indicating the new filname path that will be 0021 % used to save the variable. If not specified and a new 0022 % filename is specified, the current working directory 0023 % will be used. 0024 % force - [integer] If 0, user will be prompted to verify 0025 % filename before saving. {default: 0} 0026 % 0027 % Outputs: 0028 % erp_var - Same as input GND/GRP variable, except filename information is 0029 % updated (if a new filename and/or path were specified) and 0030 % GND/GRP.saved field is set to 'yes.' Also, file is saved to 0031 % disk. 0032 % 0033 % Note: 0034 % -For some reason, the GUI doesn't work properly on Macs. Specifically, 0035 % the file selection based on file extension doesn't work. 0036 % 0037 % 0038 % Examples: 0039 % To save a GND variable with the GUI: 0040 % >>GND=save_erps(GND,'gui'); 0041 % 0042 % To save a GND variable to a new filename: 0043 % >>GND=save_erps(GND,'yngvob.GND'); 0044 % 0045 % To save a GRP variable using the filename and filepath currently stored 0046 % in the GRP variable: 0047 % >>GRP=save_erps(GRP); 0048 % 0049 % Author: 0050 % David Groppe 0051 % Kutaslab, 3/2010 0052 0053 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%% 0054 % 0055 % 3/11/10 Added force option, GUI, and updates GND.saved field to 'yes'. 0056 % 0057 % 3/15/10 Function checks to make sure path exists before attempting save. 0058 % 0059 % 3/23/10 Changed from saveGND.m to save_erps.m as function now works with 0060 % GRP variables too 0061 % 0062 % 4/4/2011: Made compatible with Windows. 0063 % 0064 % 5/24/2011: It turns out the code wasn't fully compatible with Windows. It 0065 % was inserting / instead of \ for Windows file paths. That problem has 0066 % been fixed. Thanks to Elisa Filevich for alerting us to the problem (and 0067 % the solution). 0068 % 0069 0070 if nargin<4, 0071 force=0; 0072 end 0073 0074 try 0075 %determine what type of variable erp_var is 0076 fldnames=fieldnames(erp_var); 0077 catch 0078 error('The first argument of save_erps.m needs to be a GND or GRP variable.'); 0079 end 0080 if ismember('group_desc',fldnames), 0081 var_type='GRP'; 0082 else 0083 var_type='GND'; 0084 end 0085 0086 %Path slash convention 0087 if ismac || isunix 0088 %Mac or Unix OS 0089 slash='/'; 0090 else 0091 %Windows OS 0092 slash='\'; 0093 end 0094 0095 gui=0; 0096 if nargin>1, 0097 %More than GND/GRP variable specified. File name or path will be 0098 %changed 0099 if strcmpi(new_fname,'gui'), 0100 gui=1; 0101 end 0102 0103 %Get Path 0104 if nargin==2 || isempty(new_fname_path), 0105 %new filname specified, but not a new path; use pwd 0106 new_fname_path=[pwd slash]; 0107 end 0108 if new_fname_path(end)~=slash, 0109 new_fname_path=[new_fname_path slash]; 0110 end 0111 if ~gui 0112 erp_var.filepath=new_fname_path; 0113 erp_var.filename=new_fname; 0114 end 0115 full_fname=[new_fname_path new_fname]; %not used if GUI called 0116 else 0117 %Use file name and path already stored in GND/GRP variable 0118 if isempty(erp_var.filepath), 0119 %Path not saved with GND/GRP variable for some reason 0120 %use current working directory 0121 erp_var.filepath=[pwd slash]; 0122 end 0123 new_fname_path=erp_var.filepath; 0124 new_fname=erp_var.filename; 0125 full_fname=[new_fname_path new_fname]; 0126 end 0127 0128 if ~isempty(full_fname) && ~gui, 0129 %file name and path specified or taken from GND/GRP variable 0130 if force, 0131 resp='y'; 0132 else 0133 resp=[]; 0134 end 0135 while ~(strcmpi(resp,'y') || strcmpi(resp,'yes') || strcmpi(resp,'n') || strcmpi(resp,'no')) 0136 %resp=input(sprintf('Save %s variable as %s? (y or n) ',var_type,full_fname),'s'); 0137 fprintf('Save %s variable as %s? (y or n)',var_type,full_fname); 0138 resp=input('','s'); 0139 if ~(strcmpi(resp,'y') || strcmpi(resp,'yes') || strcmpi(resp,'n') || strcmpi(resp,'no')) 0140 fprintf('Please respond with just the letter "n" or the letter "y" (no quotes).\n'); 0141 end 0142 end 0143 if strcmpi(resp,'y') || strcmpi(resp,'yes'), 0144 erp_var.saved='yes'; 0145 if ismac || isunix 0146 %Mac or Unix OS 0147 [s ss]=unix(['ls ' new_fname_path]); 0148 %Note, s will be 0 if the path exists and 1 otherwise (i.e., 1 0149 %indicates an error with the command) 0150 else 0151 %must be a PC 0152 if isdir(new_fname_path), 0153 s=0; %path exists 0154 else 0155 s=1; %path doesn't exist 0156 end 0157 end 0158 if s, 0159 %path doesn't exist 0160 watchit(sprintf('Path %s does NOT exist.',new_fname_path)); 0161 fprintf('Saving variable to disk aborted. %s variable NOT saved to disk.\n',var_type); 0162 else 0163 if strcmpi(var_type,'GRP'), 0164 GRP=erp_var; 0165 else 0166 GND=erp_var; 0167 end 0168 save(full_fname,var_type); 0169 fprintf('%s variable successfully saved to disk.\n',var_type); 0170 end 0171 else 0172 fprintf('Saving variable to disk aborted. %s variable NOT saved to disk.\n',var_type); 0173 end 0174 else 0175 %Create GUI 0176 if strcmpi(var_type,'GRP'), 0177 [jname, jpath]=uiputfile({'*.GRP','*.GRP files'; ... 0178 '*.*','All files'},sprintf('Save GRP variable as:','untitled.GRP')); 0179 else 0180 [jname, jpath]=uiputfile({'*.GND','*.GND files'; ... 0181 '*.*','All files'},sprintf('Save GND variable as:','untitled.GND')); 0182 end 0183 if ~jpath, 0184 fprintf('Output filename selection cancelled. %s variable NOT saved to disk.\n',var_type); 0185 else 0186 erp_var=save_erps(erp_var,jname,jpath,1); % 1 means that user won't be asked again about saving file 0187 end 0188 end 0189