reading a data file with lines of different length
이전 댓글 표시
I am trying to translate a program written in Pascal into Matlab. I am blocked with the opening and reading of a data file which is attached here.
FILE
2
9
5.8688000000000000E+0000 3.7543377578475808E+0000 2.3741040000000000E+0001
4.6882400000000000E-0002 6.8480000000000000E-0002 1.0378390189135331E-0001 7.837578058E-01
4.3095200000000000E-0001 4.6940000000000000E-0001 1.5901029887692748E-0001
5.4136920610605448E-0002 8.6036251581137500E-0002 1.7313773960029888E-0001
1.4000000000000000E+0002
The problem is that the length of each line is different, and the extension is not txt or dat (and I cannot change it).
How can I save the variables so I can use them afterwards? thanks
답변 (1개)
Andrei Bobrov
2012년 6월 26일
f = fopen('yourfile.txt');
c = textscan(f,repmat('%f ',1,4));
fclose(f);
m = cellfun(@numel,c);
k = arrayfun(@(x,y)[x{1};nan(max(m)-y,1)],c,m,'un',0);
out = cat(2,k{:});
카테고리
도움말 센터 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!