h5read - howto read values in hexadecimal

1 次查看(过去 30 天)
Beder
Beder 2016-11-20
评论: Beder 2016-12-6
Dear all, currently I'm trying to write h5-files containing singles into a sql-database expecting floats (single).
Matlab reads the data of the h5 files into singles. Due to the use of sprintf and a matrix containing singles and doubles I unfortunately need to change them to double. Otherwise the values of the generated sprintf gets weird.
So I'm searching for a way howto read the h5 files as hexadecimal, therefore not altering the values and transferring them in hexadecimal to my sql-database.
I would be very pleased to get help - since I did not find anything on google.
This is the code I'm using:
conn =database.ODBCConnection('FirstTryLocal','user','Password');
for mint=1:10;
maxt=mint;
countt=maxt-mint+1;
for minlat=1:36:360-35;
maxlat=minlat+35
countlat=maxlat-minlat+1;
minlon=1;
maxlon=540;
countlon=maxlon-minlon+1;
n=datenum('20120101000000','yyyymmddHHMMSS');
cutstart=[minlat minlon mint];
cutlength=[36 maxlon maxt-mint+1];
evap= double(h5read('path','/data',cutstart,cutlength));
v50= double(h5read('path','/data',cutstart,cutlength));
t=1:countt;
lat=minlat:maxlat;
lon = minlon:maxlon;
datum=round(str2num(datestr(n+(mint+t-2)./24,'yymmddHH')));
a = repmat(datum', [countlon*countlat 1]);
b = repmat(lat,[countlon countt]);
c = repmat(lon',[countt countlat]);
d = permute(evap,[2 1 3]);
m = permute(v50,[2 1 3]);
str5=sprintf('\r\n (%d%03d%03d, %d, %i, %i, %d, %d), ', [a(:)'; b(:)'; c(:)'; a(:)'; b(:)'; c(:)'; m(:)']);
%str5=sprintf('\r\n (%d), ', [a(:)']);
sqlquery = strcat('INSERT INTO datanew (ID, datum, lat, lon, evap, v50) VALUES ',str5);
insertQuery = [sqlquery(1:end-1),';'];
response=exec(conn,insertQuery);
if isempty(response.Message)
else
disp(strcat('Error during import of time ',num2str(datum(1)),' and ',num2str(datum(end)),', ',num2str(minlat),', ',num2str(maxlat),', ',num2str(minlon),', ',num2str(maxlon)));
disp(response.Message)
if strcmp(response.Message,'ODBC Driver Error: [ma-1.0.6][10.1.19-MariaDB]MySQL server has gone away')
disp('This Error appears due to the size of the SQL-Query. Please reduce the amout of data transfered. E.g. 360x540x1')
end
pause
end
end
end
toc
close(conn)
  2 个评论
Jan
Jan 2016-11-20
What does "Due to the use of sprintf and a matrix containing singles and doubles" mean? Matrices cannot contain values of different type.
Beder
Beder 2016-12-6
Hi, sorry for the late reply. Didn't see your answer.
When using sprintf with one matrix being double, all the others need to be double as well. The Matrix with the datenum is double - this is why I need to convert all my other values to double as well.

请先登录,再进行评论。

回答(1 个)

Sid Jhaveri
Sid Jhaveri 2016-12-5
1) I believe that the HDF5 file that you are reading contains singles and you would like to convert it into hexadecimal values. You can do so by using “ num2hex ” function. Thus, instead of the converting your singles into doubles, convert it into hexadecimal values.
For example, instead of using,
evap= double(h5read('path','/data',cutstart,cutlength));
use,
evap= num2hex(h5read('path','/data',cutstart,cutlength));
2) You can also try to convert the double precision values into single precision values by using “ single ” function. This way, all the numeric values in your code will be single precision.
I hope is helps.
  1 个评论
Beder
Beder 2016-12-6
Thank you very much for the information. I didn't know about num2hex. And I didn't know, that it does not somehow alter the values of the single-datatype. It helps a lot. Thank you

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by