leon
October 24, 2021, 11:16am
1
I have a unit row like the below. You see “” because some of the units are none. For example, there is no unit for Year.
row2 = ["" "" "" "umol/kg" "" "" "kg/m3" "decimal degrees"];
In reality it is much longer, so I would need to break them into different lines.
Why does the below give me an error? It seems that instead of considering it as a 1-row array, Julia is trying to do a vcat for the elements in these two lines?
row2 = ["" "" "" "umol/kg" "" ""
"kg/m3" "decimal degrees"];
Error message:
ArgumentError: number of columns of each array must match (got (6, 2))
Stacktrace:
[1] _typed_vcat(#unused#::Type{String}, A::Tuple{Matrix{String}, Matrix{String}})
@ Base ./abstractarray.jl:1553
[2] typed_vcat(::Type{String}, ::Matrix{String}, ::Matrix{String})
@ Base ./abstractarray.jl:1567
[3] typed_hvcat(::Type{String}, ::Tuple{Int64, Int64}, ::String, ::Vararg{String, N} where N)
@ Base ./abstractarray.jl:1957
[4] hvcat(::Tuple{Int64, Int64}, ::String, ::Vararg{String, N} where N)
@ Base ./abstractarray.jl:1932
[5] top-level scope
@ In[330]:1
[6] eval
@ ./boot.jl:360 [inlined]
[7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
jling
October 24, 2021, 11:22am
2
julia> [1 2 3
4 5 6]
2×3 Matrix{Int64}:
1 2 3
4 5 6
julia> row2 = ["", "", "", "umol/kg", "", "",
"kg/m3", "decimal degrees"]
8-element Vector{String}:
""
""
""
"umol/kg"
""
""
"kg/m3"
"decimal degrees"
read: Multi-dimensional Arrays · The Julia Language
1 Like
leon
October 24, 2021, 11:32am
3
Thank you for the reply!
I’m able to combine this with the below code to make it work:
row2 = reshape(row2, (1,8));
jling
October 24, 2021, 11:40am
4
you can also hack it as
julia> row2 = ["" "" "" "umol/kg" "" "\
" "kg/m3" "decimal degrees"]
1×8 Matrix{String}:
"" "" "" "umol/kg" "" "" "kg/m3" "decimal degrees"
by leaving a "
un-closed, parser will continue to next line, the \
cancels the line-breaking \n
that was suppose to be there.
1 Like
In Julia 1.7 there will also be the option
julia> row2 = ["" "" "" "umol/kg" "" "";;
"kg/m3" "decimal degrees"]
1×8 Matrix{String}:
"" "" "" "umol/kg" "" "" "kg/m3" "decimal degrees"
but I would recommend letting go of Matlab habits and just use a 1D vector.
4 Likes
leon
October 24, 2021, 12:58pm
6
Many thanks! How do I use a 1D vector in this case?
You construct it as shown earlier,
julia> vec2 = ["", "", "", "umol/kg", "", "",
"kg/m3", "decimal degrees"]
8-element Vector{String}:
""
""
""
"umol/kg"
""
""
"kg/m3"
"decimal degrees"
Using it is up to the rest of your code. It depends on what you do more exactly with it, but usually a 1D vector of strings should be at least as easy to work with as a 1×N matrix in Julia.
1 Like
You can (equivalently ) just use permutedims(row2)
Another alternative is to use a multi-line comment to absorb the line break:
row2 = ["" "" "" "umol/kg" "" "" #=
=# "kg/m3" "decimal degrees"]
#= ... =#
can generally be used for line continuation in Julia.
3 Likes
leon
October 24, 2021, 4:02pm
9
It’s great to learn this below trick! Many thanks for sharing!
#= ... =#
can generally be used for line continuation in Julia.
This is useful, but I still wouldn’t mind a line continuation syntax which doesn’t need something on both lines. Not a big deal though.
1 Like
leon
October 24, 2021, 5:08pm
11
I agree. Julia is designed to not use line breakers, but from time to time, my program would throw out an error because of breaking a line of code into multiple lines. They can usually be fixed by simply putting everything into one long line.