我在尝试从attached text file ('scratch.txt'
)中提取数据时遇到了很多问题。我的目标是将文本文件中的每一行转换为1
xn
向量,其中n
是文本文件行中的变量数。我还需要确保提取到这些向量中的每个值都是一个8字节的浮点数。
这就是我到目前为止所拥有的,但我不知道如何将当前作为输出的内容转换为矩阵:
fid = fopen('scratch.txt');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
目前,这是我得到的输出:
4 3
1 10
2 30
3 20
4 0
1 4 1
2 1 3
3 3 2
1.e7 1.339 .5
4
1 5 3 4
1
7 5.0
发布于 2016-10-26 13:34:17
使用str2num
将tline
转换为数值行向量。
由于每行中的元素数量不同,因此不能将数据转换为矩阵: matrix (根据定义)在每行中具有相同数量的元素。
您可以做的是将行存储在cell array中。
res = {};
fid = fopen('scratch.txt');
tline = fgetl(fid);
while ischar(tline)
res{end+1} = str2num(tline);
tline = fgetl(fid);
end
https://stackoverflow.com/questions/40253871
复制相似问题