简体   繁体   English

在octave / matlab中读取和解析文本文件

[英]Read and parse text file in octave/matlab

I am trying to read in some values from a file in an octave program (I suspect matlab is similar), but not sure how to do it. 我试图从八度程序中的文件读取一些值(我怀疑matlab是类似的),但不知道如何做到这一点。

I have input file in the form: 我有以下形式的输入文件:

x y
A B C
a_11 ...  a_1n
a_21 ..   a_2n
...
a_m1 ...  a_mn

Where x,y are doubles, A, B, C are integers, and a_11...a_mn is a matrix. 其中x,y是双精度数,A,B,C是整数,a_11 ... a_mn是矩阵。

I saw examples of how to read in just the matrix, but how can I read mixed stuff like this? 我看到了如何只读取矩阵的示例,但我怎样才能读到这样的混合内容?

In my opinion this is not a good way of storing data. 在我看来,这不是一种存储数据的好方法。 But octave offers the functionality to read this as well with dlmread : 但是octave提供了使用dlmread读取它的功能:

data = dlmread (file, sep, r0, c0)
data = dlmread (file, sep, range)

If you have this text file test.csv : 如果你有这个文本文件test.csv

1 2
1.1 2.2 3.3 4.4
1 2 3
4 5 6
7 8 9

You can read in your data like this: 您可以像这样读取数据:

integers = dlmread('test.csv', '', [0 0 0 1]);
floats   = dlmread('test.csv', '', [1 0 1 3]);
matrix   = dlmread('test.csv', '', 2, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM