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

4 vues (au cours des 30 derniers jours)
[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 commentaires
Stephen23
Stephen23 le 12 Mar 2021
@Tim Darrah: a bit more context (other code and processing) might help us to understand where/why this occurs.
Tim Darrah
Tim Darrah le 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.

Connectez-vous pour commenter.

Réponse acceptée

Rishik Ramena
Rishik Ramena le 18 Mar 2021
Try setting the 'ToScalar' to true in table2struct .
  5 commentaires
Tim Darrah
Tim Darrah le 18 Mar 2021
It looks like when I look at databaseImportOptions, this is actually querying the database itself, and not what was loaded from the database. So I am misrepresenting the problem then and need to update the description.
Tim Darrah
Tim Darrah le 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);

Connectez-vous pour commenter.

Plus de réponses (1)

Tim Darrah
Tim Darrah le 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
  5 commentaires
Tim Darrah
Tim Darrah le 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.
Gordon Burns
Gordon Burns le 1 Juil 2023
Using fetch() instead of select() solves this problem.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by