'Octave not recognising values when loaded from file
I have a tab separated values file which I load and convert to a tablicious table where the values are scalars.
When picking values from the table it does not recognize all values. If I create the exact same values direct as a matrix it does.(tab1.a == tab2.a)' gives ans = 1 1 0 0 0 0 1 showing that values 3 to 6 are different. Difference of 2.2204e-16 indicates it's a numeric accuracy thing, but how can I allow for that?
I'm on Octave version 6.4.0
clear all; clc;
pkg load tablicious
function pick = tabpick(tab, a, b);
pick = ((tab.a==a) & (tab.b==b));
if ~sum(pick) % no match
error("Selection not dataset !!\n");
endif
pick = table2struct( tab(pick,:));
return
endfunction
% create table direct
m = [ 1.315 1.7
1.315 2.19
1.66 2.1
1.66 2.3
1.66 3.03
1.76 3.28
1.9 2.4 ];
a = m(:,1);
b = m(:,2);
tab1 = table(a, b);
% prettyprint the table
pp(tab1)
% show type of variables
typeinfo(tab1.a), typeinfo(tab1.a(1))
% load table from tsv file
fid = fopen('table_test.txt', 'r');
tab_file = textscan(fid,"%f %f",'delimiter','\t', 'Headerlines',1);
a = tab_file{1};
b = tab_file{2};
tab2 = table(a, b);
pp(tab2)
typeinfo(tab2.a), typeinfo(tab2.a(1))
% compare the two: They are different !
(tab1.a == tab2.a)'
% we can pick any value by menu
pick=menu('pick b', cellstr(num2str(tab2.b)));
table2struct( tab2(pick,:))
% pick from tab1 works fine
tabpick(tab1, 1.315, 2.19)
tabpick(tab1, 1.76, 3.28)
% pick from tab2 fails on second one
tabpick(tab2, 1.315, 2.19)
tabpick(tab2, 1.76, 3.28)
Solution 1:[1]
The difference value gave the clue. Working with
function pick = tabpick(tab, a, b);
pick = (abs(tab.a-a)< 1e-15) & (abs(tab.b-b) <1e-15);
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robbes |
