开发者

load data from text file using matrix separator

I am trying to read a text file which contains the data for several matrices and load them in different matrices in matlab. An example of my text file looks like this:

19.623 -15.67 42.995

17.942 -10.923 47.112

23.806 -14.33开发者_StackOverflow2 49.912

21.582 -11.218 46.203

18.031 -12.567 44.381

15.931 -18.897 39.046

18.497 -17.457 44.327

17.548 -18.604 44.24

15.931 -18.897 39.046

18.43 -16.582 43.091

18.497 -17.457 44.327

16.695 -17.627 39.364

17.548 -18.604 44.24

20.436 -16.416 44.281

17.984 -16.918 44.441

14.703 -17.17 40.697

17.078 -18.111 44.22

16.322 -16.342 38.961

19.582 -18.281 39.937

14.941 -16.27 39.53

18.43 -16.582 43.091

This file represents three matrices where the first, second and third matrices dimensions are 7x3, 6x3 and 8x3 respectively. The matrices are separated using a new line. I have tried using S = LOAD(FILENAME) command but this command creates a 21x3 matrices which is concatenation of all the rows. How can I define a matrix separator (delimiter) for my matrices so it produces different matrices based on different segment of my file? Note that the number of all columns is always 3.

Thanks a million for all your help. I really appreciate it.


If you do not have NaN values in your data, you can use textscan with a \n delimiter. It produces NaN values when a blank line is read. Not very elegant but it does the trick. I am sure there are plenty of other ways of doing this.

fid=fopen('data','r');
data=textscan(fid,'%f','delimiter','\n');
fclose(fid);
data=cell2mat(data);
index=find(isnan(data));
A=data(1:index(1)-1);
B=data(index(1)+1:index(2)-1);
C=data(index(2)+1:end);
A=reshape(A,[3 length(A)/3])';
B=reshape(B,[3 length(B)/3])';
C=reshape(C,[3 length(C)/3])';

I must add that if you have any control over how these data files are created you should probably think of changing their format to either one matrix per file or a Matlab mat file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜