significant figures from h5read() output
조회 수: 7 (최근 30일)
이전 댓글 표시
I have an issue where the output of h5read() is being rounded, and I wish to increase the number of significant figures that are being saved. I need it to be more precise, because these values are used as indices for more data within the file, which is then used to group the data.
The issue is that, because of the rounding, I am not able to use these values to access the data that I need. For example, a value in the h5 file is "315.059998", which is then being stored as "315.060", which then cannot access the bin for "/bins 315.059998".
I have checked the help centre page for the h5read() function, and cannot see anything that solves my issue (though I may have missed it), and I have also made sure that the values really are stored in the h5 file at the correct precision.
If anybody knows how to increase the precision that these numbers are saved as, I would be very grateful.
A reduced example is shown below:
vals = h5read('filename.h5','/values');
for i = 1:length(vals)
output(i,:) = h5read('filename.h5',strcat('/bins',num2str(vals(i))));
end
댓글 수: 0
채택된 답변
Walter Roberson
2023년 10월 17일
str2num(vals(i)) would be a number -- an error if vals(i) is not character vector or string scalar. If it is a character vector or string scalar but cannot be converted to number then double precision [] will be returned.
When you strcat() between a character vector and the str2num() result, then the result would be as if char() had been called on the numeric value -- for example
strcat('ABC', str2num('49'))
because str2num('49') is going to return 49, and char(49) is '1'
If your actual code uses num2str() instead of str2num(), then notice in the documentation that num2str() with a single parameter choses the output precision based upon the magnitude of the inputs.
댓글 수: 7
Walter Roberson
2023년 10월 19일
In MATLAB, you used num2str(), which has a default conversion precision that does not happen to match your needs. You can explicitly use a different conversion precision such as num2str(val_double, 17)
The equivalent conversion that you are doing in Python uses a different default conversion precision than MATLAB does.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!