如何防止 Octave 在 csvread 中将空白条目作为零?
How to prevent octave taking blank entries as zeroes in csvread?
我有一个包含许多 x 和 y 列的 csv 文件,我想将其绘制在单个图形中。然而,不同列中的数据点数量不同,那些空白值在 CSV 文件中用逗号表示,Octave 将它们视为(零,零)。
如何忽略空格?
这是这样一个CSV文件(列代表x1,y1,x2,y2)
0.59,0.09,1.12,0.106
0.23,0.09,0.88,0.104
,,0.02,0.105
这是八度代码
function getdata()
M = csvread("filename.csv");
x1 = M(:,1);
y1 = M(:,2);
x2 = M(:,3);
y2 = M(:,4);
figure 1;
plot(x1,y1, "or", x2,y2, "*")
这是我得到的情节。请注意,在 (0,0) 中有一个数据点,这是我不想要的。
如果您查看 csvread
的帮助,您会发现它只是 dlmread
的包装器。
This function is equivalent to
X = dlmread (FILENAME, "," , DLM_OPT1, ...)
Any optional arguments are passed directly to 'dlmread'
dlmread
帮助描述了一个可选参数 'emptyvalue'
,它允许您指定对文件中的空条目执行的操作。
-- DATA = dlmread (FILE, SEP, RANGE)
-- DATA = dlmread (..., "emptyvalue", EMPTYVAL)
...
The "emptyvalue" option may be used to specify the value used to
fill empty fields. The default is zero. Note that any non-numeric
values, such as text, are also replaced by the "emptyvalue".
使用此参数,您可以插入任何您想要的数值。这包括诸如 Inf 和 NaN 之类的值,如果您想这样做,您可以使用它们在绘图之前过滤掉数据。
>> dlmread('testdata.dat')
ans =
0.59000 0.09000 1.12000 0.10600
0.23000 0.09000 0.88000 0.10400
0.00000 0.00000 0.02000 0.10500
>> dlmread('testdata.dat','emptyvalue',999)
ans =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
999.000000 999.000000 0.020000 0.105000
>> dlmread('testdata.dat','emptyvalue',inf)
ans =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
Inf Inf 0.020000 0.105000
>> dlmread('testdata.dat','emptyvalue',nan)
ans =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
NaN NaN 0.020000 0.105000
plot 命令似乎会同时忽略 NaN 和 inf。为此,我建议使用 NaN。
>> M = dlmread('testdata.dat','emptyvalue',nan)
M =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
NaN NaN 0.020000 0.105000
>> x1 = M(:,1); y1 = M(:,2); x2 = M(:,3); y2 = M(:,4);
>> plot(x1,y1,"or", x2,y2,"*")
我有一个包含许多 x 和 y 列的 csv 文件,我想将其绘制在单个图形中。然而,不同列中的数据点数量不同,那些空白值在 CSV 文件中用逗号表示,Octave 将它们视为(零,零)。
如何忽略空格?
这是这样一个CSV文件(列代表x1,y1,x2,y2)
0.59,0.09,1.12,0.106
0.23,0.09,0.88,0.104
,,0.02,0.105
这是八度代码
function getdata()
M = csvread("filename.csv");
x1 = M(:,1);
y1 = M(:,2);
x2 = M(:,3);
y2 = M(:,4);
figure 1;
plot(x1,y1, "or", x2,y2, "*")
这是我得到的情节。请注意,在 (0,0) 中有一个数据点,这是我不想要的。
如果您查看 csvread
的帮助,您会发现它只是 dlmread
的包装器。
This function is equivalent to
X = dlmread (FILENAME, "," , DLM_OPT1, ...)
Any optional arguments are passed directly to 'dlmread'
dlmread
帮助描述了一个可选参数 'emptyvalue'
,它允许您指定对文件中的空条目执行的操作。
-- DATA = dlmread (FILE, SEP, RANGE) -- DATA = dlmread (..., "emptyvalue", EMPTYVAL)
...
The "emptyvalue" option may be used to specify the value used to fill empty fields. The default is zero. Note that any non-numeric values, such as text, are also replaced by the "emptyvalue".
使用此参数,您可以插入任何您想要的数值。这包括诸如 Inf 和 NaN 之类的值,如果您想这样做,您可以使用它们在绘图之前过滤掉数据。
>> dlmread('testdata.dat')
ans =
0.59000 0.09000 1.12000 0.10600
0.23000 0.09000 0.88000 0.10400
0.00000 0.00000 0.02000 0.10500
>> dlmread('testdata.dat','emptyvalue',999)
ans =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
999.000000 999.000000 0.020000 0.105000
>> dlmread('testdata.dat','emptyvalue',inf)
ans =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
Inf Inf 0.020000 0.105000
>> dlmread('testdata.dat','emptyvalue',nan)
ans =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
NaN NaN 0.020000 0.105000
plot 命令似乎会同时忽略 NaN 和 inf。为此,我建议使用 NaN。
>> M = dlmread('testdata.dat','emptyvalue',nan)
M =
0.590000 0.090000 1.120000 0.106000
0.230000 0.090000 0.880000 0.104000
NaN NaN 0.020000 0.105000
>> x1 = M(:,1); y1 = M(:,2); x2 = M(:,3); y2 = M(:,4);
>> plot(x1,y1,"or", x2,y2,"*")