我有一个文件,我想加载到Octave但它不起作用。它是纯文本文件(.txt)。 该文件是同质的,从它的几行的摘录看起来像这样:
0023,225.935,341.770,17.658
0024,225.935,341.758,17.782
LTAX17,228.152,353.935,17.665
LTAX24,288.304,332.878,24.074
其中第一列描述了点的名称,而其余列表示其3D坐标。
我尝试过的一些选项(但不仅限于这些)都没有成功。
x=load(text.txt)
error: scalar cannot be indexed with .
error: evaluating argument list element number 1
x=load("-text", "text.txt")
warning: load: file found in load path
error: load: empty name keyword or no data found in file 'text.txt'
x=fileread(text.txt)
warning: load: file found in load path
error: load: empty name keyword or no data found in file 'text.txt'
我也尝试过简化文件,只保留坐标并将文件视为CSV,但我一直遇到类似的错误。
答案 0 :(得分:7)
我认为load
仅适用于数据文件,不适用于文本文件。您可以使用csvread
,dlmread
,textscan
或textread
。检查documentation是否有关于调用每个函数的正确语法。
以下是不同的方法:
load
无效,因为您发现了
x = load(' test.txt')
error: value on right hand side of assignment is undefined
csvread
有效,但会将所有非数字值转换为0
x = csvread(' test.txt')
x =
23.00000 225.93500 341.77000 17.65800
24.00000 225.93500 341.75800 17.78200
0.00000 228.15200 353.93500 17.66500
0.00000 288.30400 332.87800 24.07400
dlmread
的工作方式与csvread
x = dlmread(' test.txt')
x =
23.00000 225.93500 341.77000 17.65800
24.00000 225.93500 341.75800 17.78200
0.00000 228.15200 353.93500 17.66500
0.00000 288.30400 332.87800 24.07400
textscan
有效,结果存储在单元格数组中
fid = fopen(' test.txt');
x = textscan(fid,'%s%f%f%f','分隔符',',')
x =
{
[1,1] =
{
[1,1] = 0023
[2,1] = 0024
[3,1] = LTAX17
[4,1] = LTAX24
}
[1,2] =
225.94
225.94
228.15
288.30
[1,3] =
341.77
341.76
353.94
332.88
[1,4] =
17.658
17.782
17.665
24.074
}
>> fclose(fid);
我还没有完成textread
,但你明白了。
答案 1 :(得分:0)
尝试将数据复制到Microsoft的wordpad并将文件保存为“.dat”扩展名并将其加载到Octave中。为我工作,希望同样适合你,祝你好运!
答案 2 :(得分:0)
# Created by Octave 4.2.1, Sat Jun 03 19:48:46 2017 GMT <unknown@Asus>
# name: a
# type: matrix
# rows: 6
# columns: 3
60 30 50
20 45 65
24 23 23
23 32 32
23 65 65
34 54 45
另存为file.data
打开八度音阶
>>load file.data
>>a