Filter löschen
Filter löschen

significant figures from h5read() output

7 Ansichten (letzte 30 Tage)
Max
Max am 17 Okt. 2023
Kommentiert: Walter Roberson am 19 Okt. 2023
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Okt. 2023
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 Kommentare
Max
Max am 19 Okt. 2023
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 am 19 Okt. 2023
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by