time | calls | line |
---|
| | 1 | function out=fileread(filename)
|
| | 2 | %FILEREAD Return contents of file as string vector.
|
| | 3 | % TEXT = FILEREAD('FILENAME') returns the contents of the file FILENAME as a
|
| | 4 | % MATLAB string.
|
| | 5 | %
|
| | 6 | % See also FREAD, TEXTSCAN, LOAD, WEB.
|
| | 7 |
|
| | 8 | % Copyright 1984-2011 The MathWorks, Inc.
|
| | 9 |
|
| | 10 | % Validate input args
|
| 2 | 11 | narginchk(1, 1);
|
| | 12 |
|
| | 13 | % get filename
|
| 2 | 14 | if ~ischar(filename),
|
| | 15 | error(message('MATLAB:fileread:filenameNotString'));
|
| | 16 | end
|
| | 17 |
|
| | 18 | % do some validation
|
| 2 | 19 | if isempty(filename),
|
| | 20 | error(message('MATLAB:fileread:emptyFilename'));
|
| | 21 | end
|
| | 22 |
|
| | 23 | % open the file
|
| 2 | 24 | [fid, msg] = fopen(filename);
|
| 2 | 25 | if fid == (-1)
|
| | 26 | error(message('MATLAB:fileread:cannotOpenFile', filename, msg));
|
| | 27 | end
|
| | 28 |
|
| 2 | 29 | try
|
| | 30 | % read file
|
| 2 | 31 | out = fread(fid,'*char')';
|
| | 32 | catch exception
|
| | 33 | % close file
|
| | 34 | fclose(fid);
|
| | 35 | throw(exception);
|
| | 36 | end
|
| | 37 |
|
| | 38 | % close file
|
| 2 | 39 | fclose(fid);
|