필터 지우기
필터 지우기

significant figures from h5read() output

조회 수: 7 (최근 30일)
Max
Max 2023년 10월 17일
댓글: Walter Roberson 2023년 10월 19일
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

채택된 답변

Walter Roberson
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'))
ans = 'ABC1'
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
Max
Max 2023년 10월 19일
Hi Walter,
Thanks for all the information. You are correct that the device was supposed to record a value of 131.306. Using what you said about the values either being single precision or double precision, I was able to create kind of a "dirty" solution with a try, catch statement to account for the values which haven't been recorded as expected. The code now works correctly and is binning data as it should.
One thing that is still confusing me, though, is why this doesn't appear to be an issue in Python code that I tested. The values saved from the Python code were then able to be used to access the bins with no issues from the precision.
Thanks again for all the help!
Walter Roberson
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 CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by