|
159 | 159 |
|
160 | 160 | end |
161 | 161 |
|
162 | | - function print_mapping(obj) |
| 162 | + function print_mapping(obj, filename) |
| 163 | + % |
| 164 | + % Print to screen by default. |
| 165 | + % Otherwise can print to a file (markdown) or a json |
| 166 | + % |
| 167 | + % |
| 168 | + % USAGE:: |
| 169 | + % |
| 170 | + % map = print_mapping(filename) |
| 171 | + % |
163 | 172 |
|
164 | 173 | obj = flatten_mapping(obj); |
165 | 174 |
|
166 | | - fprintf(1, '\n'); |
| 175 | + % print to screen be default; |
| 176 | + output_is_json = false; |
167 | 177 |
|
168 | | - for i = 1:size(obj.mapping, 1) |
| 178 | + % what to separate input and output with |
| 179 | + left = ' '; |
| 180 | + separator = ' --> '; |
| 181 | + right = ' '; |
169 | 182 |
|
170 | | - input = obj.mapping(i); |
| 183 | + if nargin < 2 |
171 | 184 |
|
172 | | - if isfield(input, 'suffix') && isempty(input.suffix) || ... |
173 | | - ~isfield(input, 'suffix') |
174 | | - input.suffix = '*'; |
175 | | - end |
| 185 | + fid = 1; |
176 | 186 |
|
177 | | - if isfield(input, 'ext') && ~isempty(input.ext) |
178 | | - input.extension = '.*'; |
179 | | - end |
180 | | - if ~isfield(input, 'extension') || isempty(input.extension) |
181 | | - input.extension = '.*'; |
| 187 | + elseif ~strcmp(spm_file(filename, 'ext'), 'json') |
| 188 | + |
| 189 | + fid = fopen(filename, 'Wt'); |
| 190 | + if fid == -1 |
| 191 | + error('Unable to write file %s.', filename); |
182 | 192 | end |
183 | 193 |
|
184 | | - input = bids.File(input); |
| 194 | + left = '| '; |
| 195 | + separator = ' | '; |
| 196 | + right = ' |'; |
185 | 197 |
|
186 | | - output = obj.mapping(i).name_spec; |
| 198 | + elseif strcmp(spm_file(filename, 'ext'), 'json') |
187 | 199 |
|
188 | | - if isfield(output, 'suffix') && isempty(output.suffix) || ... |
189 | | - ~isfield(output, 'suffix') |
190 | | - output.suffix = '*'; |
191 | | - end |
| 200 | + output_is_json = true; |
192 | 201 |
|
193 | | - if isfield(output, 'ext') && ~isempty(output.ext) |
194 | | - output.extension = '.*'; |
195 | | - end |
196 | | - if ~isfield(output, 'extension') || isempty(output.extension) |
197 | | - output.extension = '.*'; |
| 202 | + else |
| 203 | + |
| 204 | + fid = 1; |
| 205 | + |
| 206 | + end |
| 207 | + |
| 208 | + header = ['<!--\n', ... |
| 209 | + ' THIS FILE IS AUTOMATICALLY GENERATED!\n', ... |
| 210 | + ' DO NOT EDIT MANUALLY!\n', ... |
| 211 | + '-->\n', ... |
| 212 | + '# Mapping\n\n', ... |
| 213 | + left, 'input', separator, 'output', right, '\n', ... |
| 214 | + left, '-', separator, '-', right, '\n']; |
| 215 | + if ~output_is_json |
| 216 | + fprintf(fid, '\n'); |
| 217 | + if fid ~= 1 |
| 218 | + fprintf(fid, header); |
198 | 219 | end |
| 220 | + end |
| 221 | + |
| 222 | + for i = 1:size(obj.mapping, 1) |
| 223 | + |
| 224 | + if ~output_is_json |
| 225 | + %% |
| 226 | + input = obj.mapping(i); |
| 227 | + input = prepare_for_printing(input); |
| 228 | + |
| 229 | + %% |
| 230 | + output = obj.mapping(i).name_spec; |
| 231 | + output = prepare_for_printing(output); |
199 | 232 |
|
200 | | - output = bids.File(output); |
201 | | - output.re; |
| 233 | + output.filename = ['*' output.filename]; |
202 | 234 |
|
203 | | - fprintf(1, [input.filename ' --> ' output.filename '\n']); |
| 235 | + if fid ~= 1 |
| 236 | + input.filename = strrep(input.filename, '*', '\*'); |
| 237 | + output.filename = strrep(output.filename, '*', '\*'); |
| 238 | + end |
| 239 | + |
| 240 | + fprintf(fid, '%s%s%s%s%s\n', ... |
| 241 | + left, input.filename, separator, output.filename, right); |
| 242 | + end |
204 | 243 |
|
205 | 244 | end |
206 | 245 |
|
207 | | - fprintf(1, '\n'); |
| 246 | + if ~output_is_json |
| 247 | + fprintf(fid, '\n'); |
| 248 | + if fid ~= 1 |
| 249 | + fclose(fid); |
| 250 | + end |
| 251 | + end |
208 | 252 |
|
209 | 253 | end |
210 | 254 |
|
@@ -310,3 +354,20 @@ function print_mapping(obj) |
310 | 354 |
|
311 | 355 | end |
312 | 356 | end |
| 357 | + |
| 358 | +function bf = prepare_for_printing(spec) |
| 359 | + |
| 360 | + if isfield(spec, 'suffix') && isempty(spec.suffix) || ... |
| 361 | + ~isfield(spec, 'suffix') |
| 362 | + spec.suffix = '*'; |
| 363 | + end |
| 364 | + |
| 365 | + if isfield(spec, 'ext') && ~isempty(spec.ext) |
| 366 | + spec.extension = '.*'; |
| 367 | + end |
| 368 | + if ~isfield(spec, 'extension') || isempty(spec.extension) |
| 369 | + spec.ext = '.*'; |
| 370 | + end |
| 371 | + |
| 372 | + bf = bids.File(spec); |
| 373 | +end |
0 commit comments