problems with import txt files as Matrix

1 次查看(过去 30 天)
Hello guys,
I want to import a txt file in Matlab as matrix. The txt-file looks like:
0,5 1,2636e-003 -60,13 6,2933e-004 -1,0958e-003
1, 2,4385e-003 -112,91 -9,4937e-004 -2,2461e-003
1,5 2,6536e-003 -118,09 -1,2496e-003 -2,341e-003
2, 2,9698e-003 -123,29 -1,6301e-003 -2,4824e-003
2,5 3,4171e-003 -128,32 -2,1187e-003 -2,681e-003
3, 4,0399e-003 -133,03 -2,7566e-003 -2,9533e-003
3,5 4,9076e-003 -137,33 -3,6084e-003 -3,3262e-003
4, 6,1347e-003 -141,19 -4,7805e-003 -3,8448e-003
4,5 7,9257e-003 -144,62 -6,4617e-003 -4,5894e-003
5, 1,0681e-002 -147,63 -9,0215e-003 -5,718e-003
5,5 1,5305e-002 -150,29 -1,3292e-002 -7,5864e-003
6, 2,4349e-002 -152,62 -2,1621e-002 -1,1197e-002
6,5 4,886e-002 -154,69 -4,417e-002 -2,089e-002
7, 0,31779 -156,53 -0,2915 -0,12656
7,5 8,883e-002 21,81 8,2472e-002 3,3003e-002
8, 4,3338e-002 20,295 4,0648e-002 1,5032e-002
8,5 3,0694e-002 18,887 2,9042e-002 9,9359e-003
9, 2,5012e-002 17,549 2,3848e-002 7,5416e-003
9,5 2,2021e-002 16,238 2,1142e-002 6,1578e-003
10, 2,0453e-002 14,9 1,9765e-002 5,2591e-003
10,5 1,9533e-002 13,7 1,8978e-002 4,6264e-003
11, 1,9718e-002 12,136 1,9278e-002 4,1454e-003
11,5 2,1811e-002 9,8317 2,149e-002 3,7243e-003
12, 3,1873e-002 5,5674 3,1722e-002 3,0922e-003
12,5 3,5366e-002 170,88 -3,4919e-002 5,6045e-003
So used this:
type F50.txt;
M= readmatrix('F50.txt')
As result i get this:
M =
1.0e+04 *
0 NaN NaN NaN NaN 0.0001
0.0001 0.0002 NaN NaN NaN 0.0002
0.0001 NaN NaN NaN NaN 0.0000
0.0002 0.0002 NaN NaN NaN 0.0005
0.0002 NaN NaN NaN NaN 0.0001
0.0003 0.0004 NaN NaN NaN 0.0010
0.0003 NaN NaN NaN NaN 0.0003
0.0004 0.0006 NaN NaN NaN 0.0008
0.0004 NaN NaN NaN NaN 0.0006
0.0005 0.0001 NaN NaN NaN 0.0001
0.0005 NaN NaN NaN NaN 0.0006
0.0006 0.0002 NaN NaN NaN 0.0012
0.0006 NaN NaN NaN NaN 0.0001
0.0007 0 NaN NaN NaN 1.2656
0.0007 NaN NaN NaN NaN 0.0030
0.0008 0.0004 NaN NaN NaN 0.0050
0.0008 NaN NaN NaN NaN 0.0009
0.0009 0.0002 NaN NaN NaN 0.0005
0.0009 NaN NaN NaN NaN 0.0002
0.0010 0.0002 NaN NaN NaN 0.0003
0.0010 NaN NaN NaN NaN 0.0006
0.0011 0.0001 NaN NaN NaN 0.0001
How can i prevent factorizing like this.
Other Question is, how can i export txt files without the headlines? i only need the numbers.
Maybe you have a better method to import txt files.
Thanks for the help.

采纳的回答

Rik
Rik 2019-9-25
编辑:Rik 2019-9-25
Read the file as text (e.g. with the textscan function), replace the comma with a point and convert the char array to double. The code below should help.
fid=fopen('data.txt','r');
data=textscan(fid,'%s\t%s\t%s\t%s\t%s','HeaderLines',1);
fclose(fid);
data=[data{:}];%unwrap
%replace , with . and convert char to double
fun=@(str) str2double(strrep(str,',','.'));
output=cellfun(fun,data);
  2 个评论
Aj
Aj 2019-9-25
Sorry,
Can u explain it little bit more, i am a beginner in matlab.
Rik
Rik 2019-9-25
编辑:Rik 2019-9-25
You have a question posted on this forum almost two years ago, so I don't think 'beginner' is a valid description.
See my edited answer for one way to do this.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by