MATLAB Answers

how to read undelimited ascii data?

3 views (last 30 days)
Leslie
Leslie on 8 Jul 2011
I have ascii data files which contain 144*72=10368 floating point numbers with the form xxxxxx.ddd (f10.3 for us FORTRAN folks). There are no delimiters; the numbers are just strung together in a long line. How can I read this with MATLAB? Here are things I've tried:
maxlat = 72
maxlon = 144
infile1 = fopen(strcat(file_directory,file2read)) % returns "3"
%A = fscanf(infile1(1,:),'10368%10.3f',[maxlon,maxlat]); % yields numel(A)=0
%A = fscanf(infile1,'10368%10.3f',[maxlon,maxlat]); % yields numel(A)=0
%A = fscanf(infile1,'%10.3f',[maxlon,maxlat]); % yields numel(A)=0
%A = fscanf(infile1(1,:),'%10.3f',[maxlon,maxlat]); % yields numel(A)=0
%A = textscan(infile1(1,:),'%10.3f',maxlon*maxlat); % gives A the right size, no content
%A = textscan(infile1,'%10.3f',maxlon*maxlat) % right size, no content
A = textscan(infile1,'%10.3f') % right size, no content
% Diagnostics
[nrow,ncol] = size(A) % returns nrow=1, ncol=1
A1 = A(1) % after textscan, returns "[10368x1 double]"
A3 = A(1:3)
Asub = A(1:3,1:3)

  0 Comments

Sign in to comment.

Answers (1)

Titus Edelhofer
Titus Edelhofer on 9 Jul 2011
Hi,
I would do the following (without having tried):
%read into one string:
str = fread(infile1, inf, '*char');
% break:
str = reshape(str, 11, 10368)';
% convert into cell array:
strCell = cellstr(str);
% and read the entries:
A = zeros(10368,1);
for i=1:length(A)
A(i) = str2double(strCell{i});
end
Titus

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!