how to read undelimited ascii data?

조회 수: 1 (최근 30일)
Leslie
Leslie 2011년 7월 8일
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)

답변 (1개)

Titus Edelhofer
Titus Edelhofer 2011년 7월 9일
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

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by