简体   繁体   English

如何从文件中读取一行并将其转换为数字数组? (八度)

[英]How do I read a single line from file and transform it to number array? (octave)

I have a text file with this format: 我有一个具有这种格式的文本文件:

number_of_lines
x1_values y1_values
x2_values y2_values
.
.
.
xn_values yn_values

After I scanned number_of_lines, how can I read the rest line by line and separate them into xk, respectively yk? 扫描number_of_lines后,如何逐行读取其余部分并将它们分别分为xk和yk?

(If there are 8 values on a line, the first 4 will be x's and the other 4 will be y's. There are only numbers.) (如果一行上有8个值,则前4个为x,其他4个为y。只有数字。)

I created a file "yourdata": 我创建了一个文件“ yourdata”:

5
10 20
20 30
30 25
40 55
50 60

then use for example fscanf, or textread with headerlines or fscanf and textscan. 然后使用例如fscanf或带标题行的textread或fscanf和textscan。

fid = fopen ("yourdata", "r");
lines = fscanf (fid, "%d", 1)
[d] = fscanf (fid, "%f", [lines, 2]);
fclose (fid);

x = d(:, 1)
y = d(:, 2)

gives

lines =  5
x =

   10
   20
   20
   30
   30

y =

   25
   40
   55
   50
   60

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

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