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

6 views (last 30 days)
[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.
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 .
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 shows 'double' as in the above screenshots
% this shows single???
battery = table2struct(battery, 'ToScalar', true);
% this shows single too
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.


Find more on Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!