1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
function [C, len] = sstextread (filename, as_cell)
%SSTEXTREAD read a text file as a char array or cell array of strings
%
% [C, len] = sstextread (filename, as_cell)
%
% If the longest line is > 1024 characters and as_cell is true, then C is
% returned as a cell array of strings. Otherwise it is returned as char array.
% If as_cell is not present it defaults to true. The length of the longest
% line is returned in len.
% SuiteSparseCollection, Copyright (c) 2006-2019, Timothy A Davis.
% All Rights Reserved.
% SPDX-License-Identifier: GPL-2.0+
if (nargin < 2)
as_cell = true ;
end
% first, determine # of lines and the longest line in the file
f = fopen (filename) ;
if (f < 0)
error (['cannot open ' filename]) ;
end
len = 0 ;
nline = 0 ;
while (1)
s = fgetl (f) ;
if (~ischar (s))
break
end
% ignore trailing blanks
s = deblank (s) ;
len = max (len, length (s)) ;
nline = nline + 1 ;
end
fclose (f) ;
% reopen the file
f = fopen (filename) ;
if (f < 0)
error (['cannot open ' filename]) ;
end
if (len > 1024 && as_cell)
% read the file as a cell array of strings
fprintf ('%s as cell, len: %d\n', filename, len) ;
C = cell (nline, 1) ;
i = 0 ;
while (1)
s = fgetl (f) ;
if (~ischar (s))
break
end
s = deblank (s) ;
i = i + 1 ;
len = length (s) ;
if (len == 0)
s = char (zeros (1,0)) ;
end
C {i} = s ;
end
else
% read in the file as a char array
fprintf ('%s as char, len: %d\n', filename, len) ;
C = repmat (' ', nline, len) ;
i = 0 ;
while (1)
s = fgetl (f) ;
if (~ischar (s))
break
end
s = deblank (s) ;
i = i + 1 ;
len = length (s) ;
if (len > 0)
C (i, 1:len) = s ;
end
end
end
fclose (f) ;
|