This is a static copy of a profile reportHome
importdata>parse (4 calls, 9.662 sec)
Generated 05-Aug-2011 13:00:36 using cpu time.
subfunction in file /usr/local/MATLAB/R2011a/toolbox/matlab/iofun/importdata.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
385 | Data = textscan(fileString,for... | 4 | 7.837 s | 81.1% |  |
321 | [numDataCols, numHeaderRows, n... | 4 | 0.907 s | 9.4% |  |
345 | Data = textscan(fileString,'%[... | 4 | 0.874 s | 9.0% |  |
493 | numericData = TrimTrailing(@(x... | 4 | 0.011 s | 0.1% |  |
490 | end | 4 | 0.011 s | 0.1% |  |
All other lines | | | 0.022 s | 0.2% |  |
Totals | | | 9.662 s | 100% | |
Children (called functions)
Code Analyzer results
Coverage results
[ Show coverage for parent directory ]
Total lines in function | 205 |
Non-code lines (comments, blank lines) | 68 |
Code lines (lines that can run) | 137 |
Code lines that did run | 61 |
Code lines that did not run | 76 |
Coverage (did run/can run) | 44.53 % |
Function listing
time calls line
290 function [numericData, textData, numHeaderRows] = parse(fileString, ...
291 delim, headerLines, bWarnOnMismatch)
292 %This is the function which takes in a string and parses it into
293 %"spreadsheet" type data.
4 294 error(nargchk(1,4,nargin, 'struct'));
295
4 296 numericData = [];
4 297 textData = {};
4 298 numHeaderRows = 0;
299
300 % gracefully handle empty
4 301 if isempty(fileString) && isempty(regexp(fileString,'\S','once'));
302 %regexp is faster than all(isspace(fileString));
303 return;
304 end
305
306 % validate delimiter
4 307 if length(delim.printed) > 1
308 error(message('MATLAB:importdata:InvalidDelimiter'))
309 end
310
4 311 if nargin < 3
312 headerLines = NaN;
313 end
314
315 %Arbitrarily set the maximum size for a line, used to calculate this but it
316 %was slow, so we went with all in one line or the old maximum it would
317 %check.
4 318 bufsize = min(1000000, max(numel(fileString),100)) + 5;
319
320 % use what user asked for header lines if specified
0.91 4 321 [numDataCols, numHeaderRows, numHeaderCols, numHeaderChars] = ...
322 analyze(fileString, delim, headerLines, bufsize);
323
324 % fetch header lines and look for a line of column headers
4 325 headerLine = {};
4 326 headerData = {};
4 327 origHeaderData = headerData;
4 328 useAsCells = 1;
329
4 330 if numHeaderRows
4 331 firstLineOffset = numHeaderRows - 1;
4 332 if firstLineOffset > 0
333 headerData = textscan(fileString,'%[^\n\r]',firstLineOffset,...
334 'whitespace','','delimiter','\n','bufsize', bufsize);
335 origHeaderData = headerData{1};
336 if numDataCols
337 headerData = [origHeaderData, cell(length(origHeaderData), numHeaderCols + numDataCols - 1)];
338 else
339 headerData = [origHeaderData, cell(length(origHeaderData), numHeaderCols)];
340 end
4 341 else
4 342 headerData = emptyCharCell(0, numHeaderCols + numDataCols);
4 343 end
344
0.87 4 345 Data = textscan(fileString,'%[^\n\r]',1,'headerlines',firstLineOffset,...
346 'delimiter',delim.requested,'bufsize', bufsize);
347
4 348 headerLine = Data{1};
4 349 origHeaderLine = headerLine;
350
4 351 useAsCells = 0;
352
4 353 if ~isempty(delim.printed) && ~isempty(headerLine) && ~isempty(strfind(deblank(headerLine{:}), delim.printed))
4 354 cellLine = split(headerLine{:}, delim);
355 %Trailing spaces are not treated as extra delimiters
4 356 if (delim.printed ~= ' ' && isequal(headerLine{:}(end),delim.printed))
357 cellLine(end+1) = {''};
358 end
4 359 if length(cellLine) == numHeaderCols + numDataCols
4 360 headerLine = cellLine;
4 361 useAsCells = 1;
4 362 end
4 363 end
364
4 365 if ~useAsCells
366 if numDataCols
367 headerLine = [origHeaderLine, emptyCharCell(1, numHeaderCols + numDataCols - 1)];
368 else
369 headerLine = [origHeaderLine, emptyCharCell(1, numHeaderCols)];
370 end
371 end
4 372 end
373
4 374 formatString = [repmat('%q', 1, numHeaderCols) repmat('%n', 1, numDataCols)];
375
376
377 % now try for the whole shootin' match
0.01 4 378 try
4 379 if numDataCols
380 %When the delimiter is a space, multiple spaces do NOT mean
381 %multiple delimiters. Thus, call textsscan such that it will
382 %treat them as one.
383
4 384 if isequal(delim.printed,' ')
7.84 4 385 Data = textscan(fileString,formatString,'headerlines',numHeaderRows,...
386 'CollectOutput', true, 'bufsize', bufsize);
387 else
388 Data = textscan(fileString,formatString,'delimiter',delim.requested,...
389 'bufsize', bufsize, 'headerlines',numHeaderRows, 'CollectOutput', true);
390 end
4 391 if (numHeaderCols)
392 numericData = Data{2};
4 393 else
4 394 numericData = Data{1};
4 395 end
4 396 end
4 397 wasError = false;
398 catch exception %#ok
399 wasError = true;
400 end
401
402
4 403 if nargout > 1
4 404 if numHeaderCols > 0
405 textData = emptyCharCell(size(Data{1}, 1), numDataCols+numHeaderCols);
406 textData(:, 1:numHeaderCols) = Data{1};
407 end
408
4 409 if ~isempty(headerLine)
4 410 textData = [headerLine; textData];
4 411 end
412
4 413 if ~isempty(headerData)
414 textData = [headerData; textData];
415 end
4 416 end
4 417 clear('Data');
418
4 419 if (numDataCols && numHeaderCols && (size(textData, 1) ~= numHeaderRows + size(numericData, 1)))
420 wasError = true;
421 end
422
423 % if the first pass failed to read the whole shootin' match, try again using the character offset
4 424 if wasError && numHeaderChars
425 wasError = false;
426
427 % rebuild format string
428 formatString = ['%' num2str(numHeaderChars) 'c' repmat('%n', 1, numDataCols)];
429
430 %When the delimiter is a space, multiple spaces do NOT mean
431 %multiple delimiters. Thus, call textscan such that it will
432 %treat them as one.
433 if isequal(delim.printed,' ')
434 Data = textscan(fileString,formatString,'headerlines',numHeaderRows,...
435 'returnonerror',1, 'CollectOutput', true);
436 else
437 Data = textscan(fileString,formatString,'delimiter',delim.requested,...
438 'headerlines',numHeaderRows,'returnonerror',1,'CollectOutput', true);
439 end
440
441 textCharData = Data{1};
442 numericData = Data{2};
443 numHeaderCols = 1;
444 if ~isempty(numericData)
445 numRows = size(numericData, 1);
446 else
447 numRows = length(textCharData);
448 end
449
450 if numDataCols
451 headerData = [origHeaderData, ...
452 emptyCharCell(length(origHeaderData), numHeaderCols + numDataCols - 1)];
453 else
454 headerData = [origHeaderData, ...
455 emptyCharCell(length(origHeaderData), numHeaderCols)];
456 end
457
458 if ~useAsCells
459 if numDataCols
460 headerLine = [origHeaderLine, ...
461 emptyCharCell(1, numHeaderCols + numDataCols - 1)];
462 else
463 headerLine = [origHeaderLine, emptyCharCell(1, numHeaderCols)];
464 end
465 end
466
467 if nargout > 1 && ~isempty(textCharData)
468 textCellData = cellstr(textCharData);
469 if ~isempty(headerLine)
470 textData = [headerLine; ...
471 textCellData(1:numRows), ...
472 emptyCharCell(numRows, numHeaderCols + numDataCols - 1)];
473 else
474 textData = [textCellData(1:numRows), ...
475 emptyCharCell(numRows, numHeaderCols + numDataCols - 1)];
476 end
477
478 if ~isempty(headerData)
479 textData = [headerData; textData];
480 end
481 end
482 end
483
4 484 if bWarnOnMismatch && wasError
485 warning(message('MATLAB:importdata:FormatMismatch'));
486 end
487
4 488 if nargout > 1 && ~isempty(textData)
4 489 textData = TrimTrailing(@(x)cellfun('isempty', x), textData);
0.01 4 490 end
491
4 492 if ~isempty(numericData)
0.01 4 493 numericData = TrimTrailing(@(x)(isnan(x)), numericData);
4 494 end
Other subfunctions in this file are not included in this listing.