Reputation: 633
I have a csv file containing many x and y columns which I want to plot in a single graph. However the number of data points are different in different columns and those blank values are represented by commas in the CSV file, and Octave is taking them to be (zero,zero).
How to ignore the blanks?
This is such a CSV file (the columns represent x1,y1,x2,y2)
0.59,0.09,1.12,0.106
0.23,0.09,0.88,0.104
,,0.02,0.105
This is the Octave code
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, "*")
This is the plot I get. Note that there is a data point in (0,0), which I don't want.
Upvotes: 1
Views: 1045
Reputation: 1610
If you look at the help for csvread
, you'll see that it's just a wrapper for dlmread
.
This function is equivalent to
X = dlmread (FILENAME, "," , DLM_OPT1, ...)
Any optional arguments are passed directly to 'dlmread'
The dlmread
help describes an optional parameter 'emptyvalue'
that allows you to specify what is done with an empty entry in the file.
-- 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".
Using this parameter, you can insert whatever numerical value you wish. This includes values such as Inf and NaN, which you can use to filter out the data before plotting if that is what you want to do.
>> 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
It appears that the plot command will ignore both NaN and inf. For this purpose, I'd recommend 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,"*")
Upvotes: 2