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 Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by