%READASCII Sample file how to read ASCII data to file. % %See also % % http://www.mathworks.com/support/tech-notes/1400/1403.html % http://www.mathworks.com/support/tech-notes/1600/1602.html % % Thorsten Hansen 2008-05-13 function data = readascii(filename) if nargin == 0 % default filename if no argument is given filename = 'square_mat.txt'; end % open the file returns a file handle fid fid = fopen(filename, 'rt'); % check status and report error if fid == -1 error(['Cannot open file ' filename '.']) end % read the header header = fgetl(fid); % read data from file [data, count] = fscanf(fid, '%i', [3 3]); data = data'; % transpose to get ordering right % close file using file handle st = fclose(fid); % check status and report error if fid == -1 error(['Cannot close file ' filename '.']) end