Hi fellows,
I have a code design problem for handling derived variables in my plotting package.
Say I already have a function plot
that can generate figures for variable a
. Let’s assume we now have another variable of the same type, b
, and we want to plot a+b
. The simplest way is of course
c = a+b
plot(c)
However, is it possible to have something like
plot(a+b)
plot("a+b") # or
?
Or in a more generalized scenario, can I parse any valid operations, e.g. a+(b*c)/2
? Two things I can think of are
- function handles
- metaprogramming
But I have no clue about the procedures. Can you provide me some suggestions and hints on how to do this? Thanks!
EDIT: The variables here are structs that I define for the actual variable values + meta data. So my real problem is probably how to define operations for my own type. Should I just import every possible basic operations like +
, -
, etc., and use multiple dispatch to define each operation on my own type? What makes things more complicated is that the struct looks like
struct myType
header::String
variable::Vector{String}
data::Array{Float64}
end
and data
is a multi-dimensional array, the first index of which corresponds to variable name in the vector order. So for example, the return object from reading a file may be myData
of type myType
, with the variable vector of string being ["a", "b"]
, and actual data being ones(2,3)
, with the first row representing “a” and the second row representing “b”. In this case, initially there is no variable name a
or b
, and I may have to first define a method for obtaining the corresponding data like a = get_variable(myData, "a")
. This is giving me trouble when dealing with derived quantities.