简体   繁体   English

八度将文件的每一行读取到矢量

[英]Octave read each line of file to vector

I have a file data.txt 我有一个文件data.txt

1 22 34 -2
3 34 -3
2
3 43 -3 2 3

And I want to load this file onto Octave as separate matrices 我想将此文件作为单独的矩阵加载到Octave

matrix1 = [1; 22; 34 ;-2]
matrix2 = [3; 34 -3]
.
.
.

How do I do this? 我该怎么做呢? I've tried fopen and fgetl, but it seems as if each character is given its own spot in the matrix. 我尝试过fopen和fgetl,但似乎每个字符在矩阵中都有自己的位置。 I want to separate the values, not the characters (it's space delimited). 我想分隔值,而不是字符(以空格分隔)。

quick and dirty: 快速又肮脏:

A = dlmread("file");

matrix = 1; # just for nice varname generation
for i = 1:size(A,1)
    name = genvarname("matrix",who());
    eval([name " = A(i,:);"]);
    eval(["[_,__," name "] = find(" name ");"]);
end

clear _ __ A matrix i

The format needs to be as you specified. 格式必须为您指定的格式。

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

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