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

조회 수: 8 (최근 30일)
[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
Stephen23
Stephen23 2021년 3월 12일
@Tim Darrah: a bit more context (other code and processing) might help us to understand where/why this occurs.
Tim Darrah
Tim Darrah 2021년 3월 12일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Rishik Ramena
Rishik Ramena 2021년 3월 18일
Try setting the 'ToScalar' to true in table2struct .
  댓글 수: 5
Tim Darrah
Tim Darrah 2021년 3월 18일
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 2021년 3월 24일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Tim Darrah
Tim Darrah 2021년 3월 18일
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
Tim Darrah
Tim Darrah 2021년 3월 22일
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 2023년 7월 1일
Using fetch() instead of select() solves this problem.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Reporting and Database Access에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by