How to initialize a unit with Unitful.jl when reading values and units from a file?

Hello,
I’m parsing a file defining values and corresponding units, for which I would like to initialize unitful variables in julia. Currently, it is not clear to me how to use the @u_str macro, when the value and unit are themselves variables (e.g. the value is a Float64 and the unit is a represented by a string), e.g.:

value = 2.0
unit = "L"
unitful_variable = @u_str(...) # ???
1 Like

You can use the uparse function for this:

value = 2.0
unit = "L"
unitful_variable = value * uparse(unit)

The uparse function is currently not mentioned in the package documentation, we should probably change that.

1 Like

Thanks a lot - I was not aware of the uparse functionality.