


save_matmk() - Saves a GND, GRP, specGND, specGRP, or tfrGND variable to
disk using its current filename or a newly specified filename.
Usage:
>> matmk_var=save_matmk(matmk_var,new_fname,new_fname_path);
Required Inputs:
matmk_var - a matlabMK GND, GRP, specGND, specGRP, TFR, tfrGND 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", "GRP", "specGND",
"specGRP", or "tfrGND"). If not specified, the
filename and path currently stored in the 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:
matmk_var - Same as input GND/GRP/specGND/specGRP/tfrGND variable, except
filename information is updated (if a new filename and/or
path were specified) and GND/GRP/specGND/specGRP.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_matmk(GND,'gui');
To save a GND variable to a new filename:
>>GND=save_matmk(GND,'yngvob.GND');
To save a GRP variable using the filename and filepath currently stored
in the GRP variable:
>>GRP=save_matmk(GRP);
Author:
David Groppe
Kutaslab, 3/2010

0001 function matmk_var=save_matmk(matmk_var,new_fname,new_fname_path,force) 0002 % save_matmk() - Saves a GND, GRP, specGND, specGRP, or tfrGND variable to 0003 % disk using its current filename or a newly specified filename. 0004 % 0005 % Usage: 0006 % >> matmk_var=save_matmk(matmk_var,new_fname,new_fname_path); 0007 % 0008 % Required Inputs: 0009 % matmk_var - a matlabMK GND, GRP, specGND, specGRP, TFR, tfrGND struct 0010 % variable 0011 % 0012 % Optional Inputs: 0013 % new_fname - ['gui' or a filename] If the string 'gui,' then a GUI 0014 % will be created that you can use to pick a filename. 0015 % Otherwise, new_fname should be a string indicating the 0016 % new filname that will be used to save the variable (by 0017 % convention this should NOT include the file path and 0018 % should end in the extension "GND", "GRP", "specGND", 0019 % "specGRP", or "tfrGND"). If not specified, the 0020 % filename and path currently stored in the variable 0021 % will be used. 0022 % new_fname_path - a string indicating the new filname path that will be 0023 % used to save the variable. If not specified and a new 0024 % filename is specified, the current working directory 0025 % will be used. 0026 % force - [integer] If 0, user will be prompted to verify 0027 % filename before saving. {default: 0} 0028 % 0029 % Outputs: 0030 % matmk_var - Same as input GND/GRP/specGND/specGRP/tfrGND variable, except 0031 % filename information is updated (if a new filename and/or 0032 % path were specified) and GND/GRP/specGND/specGRP.saved 0033 % field is set to 'yes.' Also, file is saved to disk. 0034 % 0035 % Note: 0036 % -For some reason the GUI doesn't work properly on Macs. Specifically, 0037 % the file selection based on file extension doesn't work. 0038 % 0039 % 0040 % Examples: 0041 % To save a GND variable with the GUI: 0042 % >>GND=save_matmk(GND,'gui'); 0043 % 0044 % To save a GND variable to a new filename: 0045 % >>GND=save_matmk(GND,'yngvob.GND'); 0046 % 0047 % To save a GRP variable using the filename and filepath currently stored 0048 % in the GRP variable: 0049 % >>GRP=save_matmk(GRP); 0050 % 0051 % Author: 0052 % David Groppe 0053 % Kutaslab, 3/2010 0054 0055 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%% 0056 % 0057 % 3/11/10 Added force option, GUI, and updates GND.saved field to 'yes'. 0058 % 0059 % 3/15/10 Function checks to make sure path exists before attempting save. 0060 % 0061 % 3/23/10 Changed from saveGND.m to save_erps.m as function now works with 0062 % GRP variables too 0063 % 0064 % 12/9/10 Changed from save_erps.m to save_matmk.m as function now works with spectral 0065 % variables too 0066 % 0067 % 1/19/11 Can now handle tfrGND variables 0068 % 0069 % 3/23/11 Can now handle TFR variables 0070 % 0071 % 5/24/2011 Made compatible with Windows. 0072 % 0073 % 3/5/2012 Gbor Hden made is possible to use path names with spaces in 0074 % the name (when using Linux). 0075 0076 0077 if nargin<4, 0078 force=0; 0079 end 0080 0081 %Determine what type of variable matmk_var is 0082 var_type=[]; 0083 if isfield(matmk_var,'freqs'), 0084 if isfield(matmk_var,'group_desc'), 0085 var_type='specGRP'; 0086 else 0087 var_type='specGND'; 0088 end 0089 elseif isfield(matmk_var,'tfr_fname'), 0090 var_type='TFR'; 0091 elseif isfield(matmk_var,'ftrip'), 0092 var_type='tfrGND'; 0093 elseif isfield(matmk_var,'group_desc'), 0094 var_type='GRP'; 0095 elseif isfield(matmk_var,'grands'), 0096 var_type='GND'; 0097 end 0098 if isempty(var_type), 0099 error('The first argument of save_matmk.m needs to be a GND, GRP, specGND, specGRP, TFR, or tfrGND variable.'); 0100 end 0101 0102 %Figure out which path convention is being used 0103 if ismac || isunix 0104 %Mac or Unix OS 0105 slash='/'; 0106 else 0107 %Windows OS 0108 slash='\'; 0109 end 0110 0111 gui=0; 0112 if nargin>1, 0113 %New filename, GUI, and (possibly) filepath specified 0114 if strcmpi(new_fname,'gui'), 0115 gui=1; 0116 end 0117 0118 %Get Path 0119 if nargin==2 || isempty(new_fname_path), 0120 %new filname specified, but not a new path; use pwd 0121 new_fname_path=[pwd slash]; 0122 end 0123 if new_fname_path(end)~=slash, 0124 new_fname_path=[new_fname_path slash]; 0125 end 0126 if ~gui 0127 if strcmpi(var_type,'TFR'), 0128 matmk_var.tfr_fname=[new_fname_path new_fname]; 0129 else 0130 matmk_var.filepath=new_fname_path; 0131 matmk_var.filename=new_fname; 0132 end 0133 end 0134 full_fname=[new_fname_path new_fname]; %not used if GUI called 0135 else 0136 %Use file name and path already stored in matmk variable 0137 if strcmpi(var_type,'TFR'), 0138 slash_ids=find(matmk_var.tfr_fname==slash); 0139 if isempty(slash_ids), 0140 new_fname_path=[pwd slash]; 0141 full_fname=[new_fname_path matmk_var.tfr_fname]; 0142 else 0143 new_fname_path=matmk_var.tfr_fname(1:slash_ids(end)); 0144 full_fname=matmk_var.tfr_fname; 0145 end 0146 else 0147 if isempty(matmk_var.filepath), 0148 %Path not saved with matmk variable for some reason 0149 %use current working directory 0150 matmk_var.filepath=[pwd slash]; 0151 end 0152 new_fname_path=matmk_var.filepath; 0153 full_fname=[matmk_var.filepath matmk_var.filename]; 0154 end 0155 end 0156 0157 if ~isempty(full_fname) && ~gui, 0158 %fname specified or taken from GND/GRP/etc... variable 0159 if force, 0160 resp='y'; 0161 else 0162 resp=[]; 0163 end 0164 while ~(strcmpi(resp,'y') || strcmpi(resp,'yes') || strcmpi(resp,'n') || strcmpi(resp,'no')) 0165 fprintf('Save %s variable as %s? (y or n)',var_type,full_fname); 0166 resp=input('','s'); 0167 if ~(strcmpi(resp,'y') || strcmpi(resp,'yes') || strcmpi(resp,'n') || strcmpi(resp,'no')) 0168 fprintf('Please respond with just the letter "n" or the letter "y" (no quotes).\n'); 0169 end 0170 end 0171 if strcmpi(resp,'y') || strcmpi(resp,'yes'), 0172 matmk_var.saved='yes'; 0173 if ismac || isunix 0174 %Mac or Unix OS 0175 [s ss]=unix(['ls "' new_fname_path '"']); 0176 %Note, s will be 0 if the path exists and 1 otherwise (i.e., 1 0177 %indicates an error with the command) 0178 else 0179 %must be a PC 0180 if isdir(new_fname_path), 0181 s=0; %path exists 0182 else 0183 s=1; %path doesn't exist 0184 end 0185 end 0186 if s, 0187 %path doesn't exist 0188 watchit(sprintf('Path %s does NOT exist.',new_fname_path)); 0189 fprintf('Saving variable to disk aborted. %s variable NOT saved to disk.\n',var_type); 0190 else 0191 if strcmpi(var_type,'GRP'), 0192 GRP=matmk_var; 0193 elseif strcmpi(var_type,'GND'), 0194 GND=matmk_var; 0195 elseif strcmpi(var_type,'specGND'), 0196 specGND=matmk_var; 0197 elseif strcmpi(var_type,'specGRP'), 0198 specGRP=matmk_var; 0199 elseif strcmpi(var_type,'TFR'), 0200 TFR=matmk_var; 0201 else 0202 tfrGND=matmk_var; 0203 end 0204 save(full_fname,var_type); 0205 fprintf('%s variable successfully saved to disk.\n',var_type); 0206 end 0207 else 0208 fprintf('Saving variable to disk aborted. %s variable NOT saved to disk.\n',var_type); 0209 end 0210 else 0211 %Create GUI 0212 if strcmpi(var_type,'GRP'), 0213 [jname, jpath]=uiputfile({'*.GRP','*.GRP files',; ... 0214 '*.*','All files'},sprintf('Save GRP variable as:','untitled.GRP')); 0215 elseif strcmpi(var_type,'GND'), 0216 [jname, jpath]=uiputfile({'*.GND','*.GND files'; ... 0217 '*.*','All files'},sprintf('Save GND variable as:','untitled.GND')); 0218 elseif strcmpi(var_type,'specGND'), 0219 [jname, jpath]=uiputfile({'*.specGND','*.specGND files'; ... 0220 '*.*','All files'},sprintf('Save specGND variable as:','untitled.specGND')); 0221 elseif strcmpi(var_type,'specGRP'), 0222 [jname, jpath]=uiputfile({'*.specGRP','*.specGRP files'; ... 0223 '*.*','All files'},sprintf('Save specGRP variable as:','untitled.specGRP')); 0224 elseif strcmpi(var_type,'TFR'), 0225 [jname, jpath]=uiputfile({'*.tfr','*.tfr files'; ... 0226 '*.*','All files'},sprintf('Save TFR variable as:','untitled.tfr')); 0227 else 0228 %tfrGND variable 0229 [jname, jpath]=uiputfile({'*.tfrGND','*.tfrGND files'; ... 0230 '*.*','All files'},sprintf('Save tfrGND variable as:','untitled.tfrGND')); 0231 end 0232 if ~jpath, 0233 fprintf('Output filename selection cancelled. %s variable NOT saved to disk.\n',var_type); 0234 else 0235 matmk_var=save_matmk(matmk_var,jname,jpath,1); % 1 means that user won't be asked again about saving file 0236 end 0237 end 0238