【发布时间】:2016-09-06 21:31:20
【问题描述】:
我有一个 .txt 文件,每行总是有两个值,由制表符分隔:
0 0
23 69
45 108
81 158
110 253
125 357
141 492
165 606
179 753
189 983
. .
0 0
4 31
33 38
45 89
60 115
75 166
93 201
107 216
116 291
133 366
148 480
170 631
196 720
207 994
. .
0 0
19 81
33 102
46 128
72 161
138 236
178 398
197 537
210 658
220 832
. .
0 0
24 38
40 90
71 166
99 193
等等
始终以 0 0 开头并以 . . (80次)
我想用 Matlab 读取所有数据。这是我的代码:
% Variab
line{1} = 0;
% Open files
fid = fopen(('D:\Dokumente\Studium\8. Semester\BA\Vali mit einzelenne punkten\alle.txt'), 'rt');
% Read Data
for i = 1:80
j = 1;
line = fgets(fid);
line = textscan(line,'%f %f');
while line{1} ~= '.'
digNum{i}(j) = line{1};
gewicht{i}(j) = line{2};
line = fgets(fid);
line = textscan(line,'%f %f');
j = j + 1;
end
end
如您所见,我希望将左侧数字保存为 digNum{upOneValueWhen'.'}{numberIn'Vector'} 和右侧数字 gewicht{upOneValueWhen'.'}{numberIn'Vector'}。
一切正常,但是当我到达 digNum = 46(倒数第二个“向量”)时,{upOneValueWhen'.'} 上升了一个数字。 我不知道为什么。 'i' 应该只在 '.' 之后上升。但由于某种原因,它会在这个特定的位置上升。
有什么想法吗??? 提前非常感谢您
【问题讨论】:
-
您的示例数据不会重现您的问题。
-
需要我复制粘贴所有数据吗?
-
需要一个重现问题的示例。
-
这看起来很奇怪
line = textscan(line,'%f %f'); while line{1} ~= '.'。您无法使用 textscan 将.读取为浮点数。试试textscan('. .', '%f %f');。 MATLAB 2015b -
想想这应该不是你问题的根源。这很奇怪。我在您的代码中看不到太多其他错误。也许您在编写代码时有其他想法。我的想法是检查MATLAB debugger 并确保代码符合您的要求。
标签: matlab text-files textscan