significant figures from h5read() output
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Max
el 17 de Oct. de 2023
Comentada: Walter Roberson
el 19 de Oct. de 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
0 comentarios
Respuesta aceptada
Walter Roberson
el 17 de Oct. de 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'))
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 comentarios
Walter Roberson
el 19 de Oct. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!