I am writing an application where the user may choose the length unit from among those provided by the Unitful
package. I create a struct where the chosen length unit is stored in one of the fields. I found that when I attempted to save the struct to a .jld file using the JLD
package, an error occurs. Here is a simplified MWE:
julia> using JLD, Unitful
julia> u = unit(1u"cm")
cm
julia> save("temp.jld", "u", u)
ERROR: UndefVarError: cm not defined
Stacktrace:
[1] top-level scope at REPL[3]:1
[2] eval at .\boot.jl:331 [inlined]
[3] eval at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1 [inlined]
[4] full_typename(::Base.GenericIOBuffer{Array{UInt8,1}}, ::JLD.JldFile, ::Tuple{Unitful.Unit{:Meter,�}}) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1065
[5] full_typename(::Base.GenericIOBuffer{Array{UInt8,1}}, ::JLD.JldFile, ::DataType) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1103
[6] full_typename(::JLD.JldFile, ::Type{T} where T) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1116
[7] commit_datatype(::JLD.JldFile, ::HDF5.HDF5Datatype, ::Any) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\jld_types.jl:61
[8] h5type_default(::JLD.JldFile, ::Any, ::Bool) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\jld_types.jl:349
[9] h5type at C:\Users\peter\.julia\packages\JLD\jeGJb\src\jld_types.jl:325 [inlined]
[10] write_compound(::JLD.JldFile, ::String, ::Unitful.FreeUnits{(cm,),�,nothing}, ::JLD.JldWriteSession; kargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:706
[11] write_compound at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:704 [inlined]
[12] #_write#23 at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:700 [inlined]
[13] _write at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:700 [inlined]
[14] #write#17 at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:524 [inlined]
[15] write at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:524 [inlined]
[16] (::JLD.var"#39#40"{String,Unitful.FreeUnits{(cm,),�,nothing},Tuple{}})(::JLD.JldFile) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1235
[17] jldopen(::JLD.var"#39#40"{String,Unitful.FreeUnits{(cm,),�,nothing},Tuple{}}, ::String, ::Vararg{String,N} where N; kws::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:compatible, :compress),Tuple{Bool,Bool}}}) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:243
[18] save(::FileIO.File{FileIO.DataFormat{:JLD}}, ::String, ::Unitful.FreeUnits{(cm,),�,nothing}; compatible::Bool, compress::Bool) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1233
[19] save(::FileIO.File{FileIO.DataFormat{:JLD}}, ::String, ::Unitful.FreeUnits{(cm,),�,nothing}) at C:\Users\peter\.julia\packages\JLD\jeGJb\src\JLD.jl:1230
[20] save(::String, ::String, ::Vararg{Any,N} where N; options::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\peter\.julia\packages\FileIO\ZknoK\src\loadsave.jl:118
[21] save(::String, ::String, ::Unitful.FreeUnits{(cm,),�,nothing}) at C:\Users\peter\.julia\packages\FileIO\ZknoK\src\loadsave.jl:118 [22] top-level scope at REPL[3]:1
[23] eval(::Module, ::Any) at .\boot.jl:331
[24] eval_user_input(::Any, ::REPL.REPLBackend) at c:\users\peter\appdata\local\programs\julia\julia-1.4.2\share\julia\stdlib\v1.4\REPL\src\REPL.jl:86
[25] run_backend(::REPL.REPLBackend) at C:\Users\peter\.julia\packages\Revise\XFtoQ\src\Revise.jl:1162
[26] top-level scope at none:0
[27] eval(::Module, ::Any) at .\boot.jl:331
[28] eval_user_input(::Any, ::REPL.REPLBackend) at c:\users\peter\appdata\local\programs\julia\julia-1.4.2\share\julia\stdlib\v1.4\REPL\src\REPL.jl:86
[29] run_backend(::REPL.REPLBackend) at C:\Users\peter\.julia\packages\Revise\XFtoQ\src\Revise.jl:1162
[30] top-level scope at none:0
This is with Julia v1.4.2, JLD v0.10.0, and Unitful v0.18.0.
I could extract a string representation of the unit using string(u)
and save only the string to the file, but after reading the file back in, I don’t know how to convert that string back into a Unitful
unit
.
Any help with either the file saving/reading or string conversion to unit would be appreciated!