'Double' data is loading as 'single' from database when database reports 'double'

1 view (last 30 days)
Tim Darrah
Tim Darrah on 11 Mar 2021
Commented: Tim Darrah on 24 Mar 2021
[UPDATE] please see the response to @Stephen Cobeldick for more information on the problem.
[UPDATE 2] see the code snippet below for a complete example of how to reproduce this problem.
I cant figure out what the deal is with this, the picture says it all. I'm using postgres, ODBC, windows 10. In the advanced options I have the default for "Numeric" as 'double'. In my database, the column types are 'double precision' (not float), but MATLAB keeps loading them as single.
?
  4 Comments
Tim Darrah
Tim Darrah on 12 Mar 2021
Thanks @Stephen Cobeldick for your reply, let me add some more info.
Here is the error I'm getting. Notice it is on line 67 of that file. This is just one instance, because I have several parameters that this error relates to, it just happens to throw on the first one.
Here is that file, where battery is loaded from the database and then converted from a table to a struct.
Here is the output from databaseImportOptions, which shows the double data type.
Here is the variable itself, which shows single data type. The only thing that happens between these two screenshots is a call to the function table2struct().
So, a work around is of course to cast all of my parameters to double, parameter by paremeter, struct by struct, but I have a strong suspicion that there is a better way to go about this and I just dont have enough in depth knowledge of matlab to know what is happening, or how to address it.
I hope this helps, and thank you kindly for requesting more information.

Sign in to comment.

Accepted Answer

Rishik Ramena
Rishik Ramena on 18 Mar 2021
Try setting the 'ToScalar' to true in table2struct .
  5 Comments
Tim Darrah
Tim Darrah on 24 Mar 2021
Thanks @Rishik Ramena, using sqlread did in fact result in reading the value types correctly, although I had to reimplement a few things (in the end, not much).
data = sqlread(conn,tablename);

Sign in to comment.

More Answers (1)

Tim Darrah
Tim Darrah on 18 Mar 2021
conn = database('uavtestbed2', 'postgres', get_password('#4KRx39Dn@09'));
battery_sern = 'B001';
uav_sern = 'X001';
battery = select(conn, sprintf(db_api.get_battery, uav_sern, battery_sern));
opts = databaseImportOptions(conn, 'eqc_battery_tb');
opts.VariableOptions
% opts.VariableOptions shows 'double' as in the above screenshots
class(battery(1,"v").v)
% this shows single???
battery = table2struct(battery, 'ToScalar', true);
class(battery.v)
% this shows single too
  4 Comments
Tim Darrah
Tim Darrah on 22 Mar 2021
It seems that sqlread() is a lot more limiting then using select(), and in order to perform a join operation I would first need to read both tables into memory and then use matlab functions such as strcmp() to filter by condition. But then what is the point of a database if I'm not able to use the functionality? This pretty much kills performance too.
Is this something that can be fixed in a future release of MATLAB? It seems like this is a bug (reading 'double' values as 'single') in MATLAB's implementation of select(), and the workaround is causing me to rewrite a lot of code that if the function just worked as expected, I wouldn't have to.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!